@yeyelailou/co 0.0.9 → 0.0.11
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/README.md +39 -5
- package/bin//345/205/245/345/217/243.js +48 -0
- package/index.js +30 -2
- package/package.json +17 -6
- package//345/267/245/345/205/267//345/217/221/345/270/203/co-language.json +34 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203/identify.js +74 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203/linguist/languages.yml +40 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203/lint.js +18 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203/parse.js +9 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203/server.js +117 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203//345/271/263/345/217/260/345/214/205/linux-arm64/package.json +29 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203//345/271/263/345/217/260/345/214/205/linux-x64/package.json +28 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203//346/236/204/345/273/272/345/271/263/345/217/260/345/214/205.js +183 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203//350/207/252/345/212/250/345/217/221/345/270/203.js +120 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203//350/257/206/345/210/253/346/265/213/350/257/225.js +30 -0
- package//345/267/245/345/205/267//345/217/221/345/270/203//350/257/255/350/250/200/350/257/206/345/210/253.js +11 -0
- package//345/267/245/345/205/267//347/224/237/346/200/201/cli.js +142 -44
- package//345/267/245/345/205/267//347/224/237/346/200/201//345/214/205/345/256/211/345/205/250.js +156 -0
- package//345/267/245/345/205/267//347/224/237/346/200/201//346/263/250/345/206/214/350/241/250/346/234/215/345/212/241.js +82 -15
- package//345/267/245/345/205/267//347/224/237/346/200/201//350/247/204/350/214/203.js +20 -3
- package//345/267/245/345/205/267//347/224/237/346/200/201//351/252/214/350/257/201/344/270/211/351/200/232/351/201/223.js +9 -0
- package//346/265/213/350/257/225//345/267/245/345/205/267/351/223/276/345/256/211/345/205/250/346/265/213/350/257/225.js +214 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/binary.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/bubble.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/calc.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/factorial.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/fib.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/hanoi.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/multable.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/prime.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/reverse.elf +0 -0
- package//345/267/245/345/205/267//350/207/252/344/270/276/347/274/226/350/257/221/345/231/250//350/207/252/344/270/276/351/252/214/350/257/201/wordcount.elf +0 -0
package/README.md
CHANGED
|
@@ -39,8 +39,13 @@ cocn 你好.co
|
|
|
39
39
|
| `cocn 程序.co [参数...]` | 运行程序(默认优先走原生快路径) |
|
|
40
40
|
| `cocn --编译 程序.co 输出` | 用直接机器码后端编成静态 ELF(x86-64,不依赖 gcc) |
|
|
41
41
|
| `cocn --检查 程序.co` | 能力自检:这个程序能否走原生快路径,不能则给出原因 |
|
|
42
|
+
| `cocn --help` | 显示运行、工程与注册表命令的中文帮助 |
|
|
42
43
|
| `cocn` | 进入 REPL |
|
|
43
44
|
|
|
45
|
+
包注册表默认只监听本机;需要内网共享时显式设置 `COCR_HOST=0.0.0.0`,
|
|
46
|
+
并注意当前版本尚无发布鉴权。语言识别 API 同理,默认 `127.0.0.1`,
|
|
47
|
+
可用 `HOST` 显式修改监听地址。
|
|
48
|
+
|
|
44
49
|
### 三条执行通道
|
|
45
50
|
|
|
46
51
|
| 通道 | 实现 | 说明 |
|
|
@@ -116,7 +121,7 @@ Apache-2.0,见 `LICENSE`。
|
|
|
116
121
|
|
|
117
122
|
## 开发准则
|
|
118
123
|
|
|
119
|
-
> 本文件是仓库唯一的 Markdown 文档(2026-10-28 起使用说明与开发准则合并为单一 README.md)。最后更新:2026-12-
|
|
124
|
+
> 本文件是仓库唯一的 Markdown 文档(2026-10-28 起使用说明与开发准则合并为单一 README.md)。最后更新:2026-12-06;源码版本:**0.0.11**。
|
|
120
125
|
|
|
121
126
|
---
|
|
122
127
|
|
|
@@ -421,6 +426,30 @@ coCN 是一门**用 coCN 自身编写的中文编程语言**:词法分析、
|
|
|
421
426
|
- 工具链:对标 Cargo 的 15 条 `cocn` 子命令、`co.toml`、crates.io 式注册表、注册表回退导入。
|
|
422
427
|
- 当前首批包:**20 个**,全部完成三通道验证。
|
|
423
428
|
|
|
429
|
+
### 0.0.10 / 0.0.11 变更摘要(工具链安全与开箱即用)
|
|
430
|
+
|
|
431
|
+
**修复的真实问题**
|
|
432
|
+
|
|
433
|
+
1. **包管理器目录穿越删除**:此前 `cocn remove ../../目录` 会把包名直接拼到缓存路径并递归删除。现统一由 `工具/生态/包安全.js` 校验包名(1–64 位字母/数字/`_`/`-`)与 semver 版本,并确认目标仍是缓存根的直接子目录;`install/add/update/yank/tree` 共用该边界,依赖名与锁文件条目也会校验。
|
|
434
|
+
2. **供应链校验顺序与归档安全**:安装改为先核对 SHA-256,再解压;tar 在解压前检查绝对路径、`..`、控制字符以及符号链接/硬链接/设备等特殊成员,限制 128MB 与 10000 个成员。下载使用临时文件,非 200、超时、超大小都不会污染正式缓存。
|
|
435
|
+
3. **注册表不可变发布**:重复版本在写文件之前返回 409,不再先覆盖旧 tarball;发布时核对客户端 checksum、校验包名/版本与归档,索引原子写入。默认仅监听 `127.0.0.1`,需要内网访问时显式设置 `COCR_HOST=0.0.0.0`;`COCR_STORAGE` 可覆盖存储目录。
|
|
436
|
+
4. **语言 API 的服务器文件探测**:HTTP `/identify` 只分析 JSON 中的 `source`,不再根据请求里的 `filename` 读取服务器本地文件;请求体限 2MB,`source` 类型错误、JSON 无效与解析失败返回 400,不再把用户输入错误统一报成 500。注册表与语言 API 均限制请求体/响应大小和超时。
|
|
437
|
+
5. **全新克隆的引导器查找**:CLI、Node API 与示例包三通道验证器都会回退到仓库内 `工具/发布/平台包/<平台>/bin/引导器`,不再要求先手工设置 `COCN_BOOTSTRAP` 或复制二进制。
|
|
438
|
+
6. **semver 与使用体验**:`^0.0.9` 不再错误接受 `0.1.0`,严格遵循 0.x caret 规则;新增 `co --help`、中文未知选项/缺失文件提示;`cocn publish` 排除 `.git`、`node_modules` 与本机 tarball,临时文件失败时也会清理。
|
|
439
|
+
|
|
440
|
+
**新增门禁**
|
|
441
|
+
|
|
442
|
+
```sh
|
|
443
|
+
npm run test:security # 路径穿越、tar、checksum、不可变发布、原型污染、HTTP 边界
|
|
444
|
+
npm run test:lang # 语言识别
|
|
445
|
+
npm test # 上述两项 + 38 个语言套件三通道
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
安全测试只使用系统临时目录与本机随机端口,不访问真实 `~/.cocn`,也不需要外部网络。
|
|
449
|
+
|
|
450
|
+
发布脚本自 0.0.11 起固定使用 `https://registry.npmjs.org/`(可用 `NPM_REGISTRY` 覆盖),
|
|
451
|
+
并区分 `published` 与 `staged`:前者才可幂等跳过,后者会退避重试,避免把尚在处理或待审批的版本误报为发布完成。
|
|
452
|
+
|
|
424
453
|
### 0.0.7 变更摘要(本版:原生后端 34 轮优化——推导式/匹配/dyn 分派/下标可捕获化)
|
|
425
454
|
|
|
426
455
|
**改了什么**
|
|
@@ -524,7 +553,8 @@ GRPO 损失函数按参考原样保留定义(参考训练循环未调用它)
|
|
|
524
553
|
- CLI 同步去掉 `--生成C`(它依赖已删的汇编/运行时后端)
|
|
525
554
|
|
|
526
555
|
保留的 18 个非 coCN **实现代码**文件(`.js` / `.c`)全部是**引导与分发不可删除的最小内核**
|
|
527
|
-
(另有 20 个 `co.toml` 包清单、19 个 `.sh`
|
|
556
|
+
(另有 20 个 `co.toml` 包清单、19 个 `.sh` 门禁脚本与仓库配置文件,属数据与脚本,不是语言实现);
|
|
557
|
+
迁移边界、批次和宿主 shim 口径见 [docs/co-source-migration.md](docs/co-source-migration.md):
|
|
528
558
|
|
|
529
559
|
| 文件 | 为什么暂时不能删 |
|
|
530
560
|
|------|------------------|
|
|
@@ -605,9 +635,13 @@ GRPO 损失函数按参考原样保留定义(参考训练循环未调用它)
|
|
|
605
635
|
|
|
606
636
|
平台识别:
|
|
607
637
|
|
|
638
|
+
- 语言元数据固定在 `工具/发布/co-language.json`:ID `co`、展示名 `co语言`、后缀 `.co`、MIME `text/x-co`、行注释 `#`、关键字与识别标记。
|
|
639
|
+
- `工具/发布/identify.js` 提供文件名/源码识别;CLI:`node 工具/发布/语言识别.js 文件.co`。
|
|
640
|
+
- `工具/发布/parse.js`、`工具/发布/lint.js` 提供 AST 解析与诊断包装,供 IDE、文档站或代码平台接入。
|
|
641
|
+
- `工具/发布/server.js` 提供无依赖 HTTP API:`GET /metadata`、`POST /identify`、`POST /parse`、`POST /lint`。
|
|
608
642
|
- 加 `.gitattributes`,把 `.co` 标为可识别文本并排除构建产物。
|
|
609
|
-
- 备好 Linguist 语言定义 `工具/发布/linguist/languages.yml
|
|
610
|
-
|
|
643
|
+
- 备好 Linguist 语言定义 `工具/发布/linguist/languages.yml`,给 github/linguist 提 PR 即可让 Gitee/GitHub 认得 co(只需 GitHub 账号)。
|
|
644
|
+
- 识别测试:`npm run test:lang`。
|
|
611
645
|
|
|
612
646
|
### 0.1.35 变更摘要
|
|
613
647
|
|
|
@@ -1079,7 +1113,7 @@ npm pack --dry-run
|
|
|
1079
1113
|
|
|
1080
1114
|
| 项目 | 状态 |
|
|
1081
1115
|
|------|------|
|
|
1082
|
-
| 版本 | 0.0.
|
|
1116
|
+
| 版本 | 0.0.11 |
|
|
1083
1117
|
| 首批包 | 20 个,三通道逐一验证(2026-11-09 实测 20/20) |
|
|
1084
1118
|
| 注册表 | 本地发布/安装/导入闭环已验证(`cocn-path` 实测 publish → install → 导入 → 运行) |
|
|
1085
1119
|
| 语言套件 | 38 个(2026-11-09 在 x86-64 实测 eval/vm/native 各 38/38、三通道逐字节一致) |
|
|
@@ -68,6 +68,12 @@ function 找引导器() {
|
|
|
68
68
|
if (目录 === path.dirname(目录)) break;
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
|
+
// 全新克隆后没有 node_modules,也没跑过 postinstall;仓库自带同平台
|
|
72
|
+
// 引导器,直接作为最后一级兜底,避免 npm test 在干净环境里全假失败。
|
|
73
|
+
if (平台) {
|
|
74
|
+
候选.push(path.join(PKG_DIR, '工具', '发布', '平台包', 平台, 'bin', '引导器'));
|
|
75
|
+
候选.push(path.join(PKG_DIR, '工具', '发布', '平台包', 平台, 'bin', '引导器.exe'));
|
|
76
|
+
}
|
|
71
77
|
候选.push(path.join(BIN_DIR, '引导器'));
|
|
72
78
|
候选.push(path.join(BIN_DIR, '引导器.exe'));
|
|
73
79
|
for (const c of 候选) {
|
|
@@ -111,6 +117,42 @@ function pipeExit(child, label) {
|
|
|
111
117
|
|
|
112
118
|
const userArgs = process.argv.slice(2);
|
|
113
119
|
|
|
120
|
+
if (['--help', '-h', 'help', '帮助', '--帮助'].includes(userArgs[0])) {
|
|
121
|
+
console.log([
|
|
122
|
+
'co语言(coCN)—— 让写代码像写中文作文一样自然',
|
|
123
|
+
'',
|
|
124
|
+
'用法:',
|
|
125
|
+
' co 程序.co [程序参数...] 运行程序(原生优先,自动回退解释器)',
|
|
126
|
+
' co 进入交互式 REPL',
|
|
127
|
+
' co --编译 程序.co 输出 编译为 x86-64 静态 ELF',
|
|
128
|
+
' co --检查 程序.co 检查能否走原生快路径并给出改写建议',
|
|
129
|
+
' co --version 显示版本',
|
|
130
|
+
' co --help 显示本帮助',
|
|
131
|
+
'',
|
|
132
|
+
'包与工程:',
|
|
133
|
+
' co new <包名> 创建 co.toml、程序.co、库.co、测试.co',
|
|
134
|
+
' co run [-- 参数...] 运行 co.toml 的 main(默认 程序.co)',
|
|
135
|
+
' co test 运行文件名含“测试”的 .co 文件',
|
|
136
|
+
' co build 编译当前包',
|
|
137
|
+
' co fmt 规整 .co 文件的行尾空白与空行',
|
|
138
|
+
' co doc 从函数上方的 # 注释生成 API.txt',
|
|
139
|
+
' co install [包名[@版本]] 安装依赖或指定包',
|
|
140
|
+
' co add <包名[@版本]> 添加并安装依赖',
|
|
141
|
+
' co remove <包名> 移除包与依赖声明',
|
|
142
|
+
' co update / tree 更新依赖 / 查看依赖树',
|
|
143
|
+
' co publish / search <词> 发布包 / 搜索注册表',
|
|
144
|
+
'',
|
|
145
|
+
'环境变量:',
|
|
146
|
+
' COCN_BOOTSTRAP 指定本机架构的引导器',
|
|
147
|
+
' COCN_BACKEND=eval|vm 选择解释器后端',
|
|
148
|
+
' COCN_REGISTRY 包注册表地址(默认 http://localhost:7878)',
|
|
149
|
+
' COCN_TOKEN 注册表访问令牌(可选)',
|
|
150
|
+
'',
|
|
151
|
+
'详细说明见仓库 README.md:https://gitee.com/COSMOnb666/cocn'
|
|
152
|
+
].join('\n'));
|
|
153
|
+
process.exit(0);
|
|
154
|
+
}
|
|
155
|
+
|
|
114
156
|
if (userArgs[0] === '--version' || userArgs[0] === '-v' || userArgs[0] === 'version') {
|
|
115
157
|
console.log(PKG_VERSION);
|
|
116
158
|
process.exit(0);
|
|
@@ -198,8 +240,14 @@ if (userArgs.length === 0) {
|
|
|
198
240
|
const child = spawn(BOOTSTRAP, [REPL_PATH], { stdio: 'inherit', env: process.env, cwd: SRC_DIR });
|
|
199
241
|
pipeExit(child, 'REPL');
|
|
200
242
|
} else {
|
|
243
|
+
if (userArgs[0].startsWith('-')) {
|
|
244
|
+
fail(`未知选项:${userArgs[0]}(运行 co --help 查看可用命令)`);
|
|
245
|
+
}
|
|
201
246
|
// 解析为绝对路径:原生启动器与自解释器都以绝对路径定位目标最稳
|
|
202
247
|
const absTarget = path.resolve(process.cwd(), userArgs[0]);
|
|
248
|
+
if (!fs.existsSync(absTarget) || !fs.statSync(absTarget).isFile()) {
|
|
249
|
+
fail(`找不到源文件:${userArgs[0]}\n 当前目录:${process.cwd()}\n 运行 co --help 查看用法。`);
|
|
250
|
+
}
|
|
203
251
|
const rest = userArgs.slice(1);
|
|
204
252
|
|
|
205
253
|
// —— 通道一(默认):原生启动器,编译缓存通道,毫秒级 ——
|
package/index.js
CHANGED
|
@@ -18,6 +18,9 @@ const BIN_DIR = path.join(PKG_DIR, 'bin');
|
|
|
18
18
|
const MAIN_BOOTSTRAP_PATH = path.join(BIN_DIR, process.platform === 'win32' ? '引导器.exe' : '引导器');
|
|
19
19
|
const INTERPRETER_PATH = path.join(SRC_DIR, '解释器.co');
|
|
20
20
|
const REPL_PATH = path.join(SRC_DIR, 'repl.co');
|
|
21
|
+
const coLanguage = require('./工具/发布/co-language.json');
|
|
22
|
+
const { identify: identifyCo } = require('./工具/发布/identify.js');
|
|
23
|
+
|
|
21
24
|
const PKG_VERSION = (() => {
|
|
22
25
|
try { return require('./package.json').version; } catch (e) { return '0.0.0'; }
|
|
23
26
|
})();
|
|
@@ -28,8 +31,21 @@ function platformSuffix() {
|
|
|
28
31
|
return arch ? `${platform}-${arch}` : null;
|
|
29
32
|
}
|
|
30
33
|
|
|
34
|
+
function 可执行路径(候选) {
|
|
35
|
+
try {
|
|
36
|
+
if (!fs.existsSync(候选)) return null;
|
|
37
|
+
const st = fs.statSync(候选);
|
|
38
|
+
if (!st.isFile()) return null;
|
|
39
|
+
if ((st.mode & 0o111) === 0) fs.chmodSync(候选, st.mode | 0o755);
|
|
40
|
+
return 候选;
|
|
41
|
+
} catch {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
31
46
|
function findBootstrap() {
|
|
32
|
-
|
|
47
|
+
const 主引导 = 可执行路径(MAIN_BOOTSTRAP_PATH);
|
|
48
|
+
if (主引导) return 主引导;
|
|
33
49
|
|
|
34
50
|
const suffix = platformSuffix();
|
|
35
51
|
if (suffix) {
|
|
@@ -41,17 +57,29 @@ function findBootstrap() {
|
|
|
41
57
|
const 父级 = path.join(目录, '..', 'node_modules', 子包名, 'bin', '引导器');
|
|
42
58
|
const 父级Exe = 父级 + '.exe';
|
|
43
59
|
for (const 候选 of [同级, 同级Exe, 父级, 父级Exe]) {
|
|
44
|
-
|
|
60
|
+
const 可用 = 可执行路径(候选);
|
|
61
|
+
if (可用) return 可用;
|
|
45
62
|
}
|
|
46
63
|
目录 = path.dirname(目录);
|
|
47
64
|
if (目录 === path.dirname(目录)) break;
|
|
48
65
|
}
|
|
66
|
+
// 与 CLI 入口一致:开发仓库未执行 postinstall 时,使用仓库内平台包。
|
|
67
|
+
const 仓库内 = path.join(PKG_DIR, '工具', '发布', '平台包', suffix, 'bin', '引导器');
|
|
68
|
+
const 仓库内可用 = 可执行路径(仓库内);
|
|
69
|
+
if (仓库内可用) return 仓库内可用;
|
|
70
|
+
const 仓库内Exe = 仓库内 + '.exe';
|
|
71
|
+
const 仓库内Exe可用 = 可执行路径(仓库内Exe);
|
|
72
|
+
if (仓库内Exe可用) return 仓库内Exe可用;
|
|
49
73
|
}
|
|
50
74
|
return null;
|
|
51
75
|
}
|
|
52
76
|
|
|
53
77
|
module.exports = {
|
|
54
78
|
version: PKG_VERSION,
|
|
79
|
+
/** co 语言元数据:ID、展示名、后缀、MIME、注释与关键字 */
|
|
80
|
+
language: coLanguage,
|
|
81
|
+
/** 识别文件名或源码是否为 co 语言 */
|
|
82
|
+
identifyCo,
|
|
55
83
|
/** 引导 cocn 二进制路径(未安装时为 null) */
|
|
56
84
|
binPath: findBootstrap(),
|
|
57
85
|
/** 自解释器主入口路径 */
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yeyelailou/co",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"_buildIter":
|
|
5
|
-
"description": "cocn
|
|
3
|
+
"version": "0.0.11",
|
|
4
|
+
"_buildIter": 11,
|
|
5
|
+
"description": "co语言(原 cocn/coCN)—— 让写代码像写中文作文一样自然。含自解释器、vm、原生子集编译器、20 个示例包与 Cargo 风格工具链;0.0.11 加固包管理器路径/checksum/tar 解压与语言 API 输入边界,修复全新克隆的引导器查找,并新增中文 CLI 帮助;发布脚本固定官方 npm 源并区分 staged/published。开发规范见 README.md。",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"bin": {
|
|
8
8
|
"co": "bin/cocn.js",
|
|
9
9
|
"cocn": "bin/cocn.js"
|
|
10
10
|
},
|
|
11
11
|
"optionalDependencies": {
|
|
12
|
-
"@yeyelailou/co-linux-x64": "0.0.
|
|
13
|
-
"@yeyelailou/co-linux-arm64": "0.0.
|
|
12
|
+
"@yeyelailou/co-linux-x64": "0.0.11",
|
|
13
|
+
"@yeyelailou/co-linux-arm64": "0.0.11"
|
|
14
14
|
},
|
|
15
15
|
"files": [
|
|
16
16
|
"bin/cocn.js",
|
|
@@ -28,6 +28,10 @@
|
|
|
28
28
|
"工具/原生编译器/编译器合并.co",
|
|
29
29
|
"工具/自举编译器/",
|
|
30
30
|
"工具/生态/",
|
|
31
|
+
"工具/发布/*.js",
|
|
32
|
+
"工具/发布/*.json",
|
|
33
|
+
"工具/发布/linguist/",
|
|
34
|
+
"工具/发布/平台包/*/package.json",
|
|
31
35
|
"示例包/",
|
|
32
36
|
"示例/",
|
|
33
37
|
"测试/",
|
|
@@ -35,9 +39,16 @@
|
|
|
35
39
|
],
|
|
36
40
|
"scripts": {
|
|
37
41
|
"postinstall": "node install.js",
|
|
38
|
-
"test": "bash 测试/套件三通道.sh bin/cocn.js"
|
|
42
|
+
"test": "npm run test:security && npm run test:lang && bash 测试/套件三通道.sh bin/cocn.js",
|
|
43
|
+
"test:security": "node 测试/工具链安全测试.js",
|
|
44
|
+
"test:lang": "node 工具/发布/识别测试.js"
|
|
39
45
|
},
|
|
40
46
|
"keywords": [
|
|
47
|
+
"co",
|
|
48
|
+
".co",
|
|
49
|
+
"co语言",
|
|
50
|
+
"co-language",
|
|
51
|
+
"language-recognition",
|
|
41
52
|
"coCN",
|
|
42
53
|
"中文编程",
|
|
43
54
|
"编程语言",
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "co",
|
|
3
|
+
"displayName": "co语言",
|
|
4
|
+
"aliases": ["co", "co语言", "coCN"],
|
|
5
|
+
"extensions": [".co"],
|
|
6
|
+
"filenames": [],
|
|
7
|
+
"mimeType": "text/x-co",
|
|
8
|
+
"lineComment": "#",
|
|
9
|
+
"stringQuotes": ["\""],
|
|
10
|
+
"keywords": [
|
|
11
|
+
"让", "变量", "常量", "整数", "浮点", "布尔", "字符串", "列表", "字典",
|
|
12
|
+
"函数", "返回", "如果", "否则", "否则如果", "否则若", "若", "当", "对于",
|
|
13
|
+
"从", "到", "步长", "循环", "结束", "跳出", "继续", "真", "假", "且", "或",
|
|
14
|
+
"非", "导入", "匹配", "情况", "是", "默认", "于", "在", "那么", "尝试", "捕获",
|
|
15
|
+
"最终", "抛出", "并发", "等待", "则", "枚举", "结构体", "作为", "类",
|
|
16
|
+
"继承", "继承自", "self", "自己", "super", "空"
|
|
17
|
+
],
|
|
18
|
+
"operators": [
|
|
19
|
+
"+", "-", "*", "/", "%", "//", "**", "=", "+=", "-=", "*=", "/=", "%=",
|
|
20
|
+
"<<=", ">>=", "&=", "|=", "^=", "=>", "->",
|
|
21
|
+
"==", "!=", "<", ">", "<=", ">=", "&", "|", "^", "~", "<<", ">>"
|
|
22
|
+
],
|
|
23
|
+
"detect": {
|
|
24
|
+
"sourceKeywords": [
|
|
25
|
+
"函数", "返回", "如果", "循环", "导入", "断言相等", "测试用例",
|
|
26
|
+
"运行所有测试", "变量", "常量", "列表", "字典"
|
|
27
|
+
]
|
|
28
|
+
},
|
|
29
|
+
"runtime": {
|
|
30
|
+
"command": "co",
|
|
31
|
+
"fileArgument": true,
|
|
32
|
+
"example": "co program.co"
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const metadata = require('./co-language.json');
|
|
6
|
+
|
|
7
|
+
function extensionOf(filename) {
|
|
8
|
+
return filename ? path.extname(filename).toLowerCase() : '';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function readSource(input, options = {}) {
|
|
12
|
+
if (typeof input.source === 'string') return input.source;
|
|
13
|
+
// HTTP API 必须显式关闭文件读取,否则 POST {"filename":"/etc/passwd"}
|
|
14
|
+
// 会把服务器本地文件纳入识别/关键字统计(虽然不直接回传正文,仍属越权探测)。
|
|
15
|
+
if (options.readFile !== false && input.filename && fs.existsSync(input.filename)) {
|
|
16
|
+
return fs.readFileSync(input.filename, 'utf8');
|
|
17
|
+
}
|
|
18
|
+
return '';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function identify(input = {}, options = {}) {
|
|
22
|
+
if (!input || typeof input !== 'object' || Array.isArray(input)) input = {};
|
|
23
|
+
const filename = typeof input.filename === 'string' ? input.filename.slice(0, 4096) : '';
|
|
24
|
+
const source = readSource(input, options);
|
|
25
|
+
const extension = extensionOf(filename);
|
|
26
|
+
const extensionMatch = metadata.extensions.includes(extension);
|
|
27
|
+
const keywordHits = metadata.detect.sourceKeywords.filter((keyword) => source.includes(keyword));
|
|
28
|
+
|
|
29
|
+
if (extensionMatch) {
|
|
30
|
+
return {
|
|
31
|
+
language: metadata.id,
|
|
32
|
+
languageId: metadata.id,
|
|
33
|
+
displayName: metadata.displayName,
|
|
34
|
+
extension,
|
|
35
|
+
mimeType: metadata.mimeType,
|
|
36
|
+
matched: 'extension',
|
|
37
|
+
confidence: 0.98,
|
|
38
|
+
isValid: true,
|
|
39
|
+
keywordHits
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (keywordHits.length >= 2) {
|
|
44
|
+
return {
|
|
45
|
+
language: metadata.id,
|
|
46
|
+
languageId: metadata.id,
|
|
47
|
+
displayName: metadata.displayName,
|
|
48
|
+
extension,
|
|
49
|
+
mimeType: metadata.mimeType,
|
|
50
|
+
matched: 'keywords',
|
|
51
|
+
confidence: 0.62,
|
|
52
|
+
isValid: true,
|
|
53
|
+
keywordHits
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
language: null,
|
|
59
|
+
languageId: null,
|
|
60
|
+
displayName: null,
|
|
61
|
+
extension,
|
|
62
|
+
mimeType: null,
|
|
63
|
+
matched: 'none',
|
|
64
|
+
confidence: 0,
|
|
65
|
+
isValid: false,
|
|
66
|
+
keywordHits
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
module.exports = {
|
|
71
|
+
metadata,
|
|
72
|
+
identify,
|
|
73
|
+
identifyCo: identify
|
|
74
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# ============================================================
|
|
2
|
+
# 给 github/linguist 提 PR 新增 co 语言时用的定义(可直接粘贴)
|
|
3
|
+
#
|
|
4
|
+
# 为什么要提 PR:Gitee / GitHub / VS Code 等平台都靠 Linguist 的
|
|
5
|
+
# 语言表按扩展名识别语言。语言表里没有 co,.co 文件就被归到
|
|
6
|
+
# 「其他」——仓库语言统计不对、语法高亮缺失、代码搜索不认。
|
|
7
|
+
# Linguist 是社区维护的,往上游提一份语言定义是唯一的正规途径
|
|
8
|
+
# (只需一个 GitHub 账号,无需任何组织权限)。
|
|
9
|
+
#
|
|
10
|
+
# 提 PR 的步骤(见《开发准则》「让更多平台认识 co 语言」一节):
|
|
11
|
+
# 1. fork github/linguist
|
|
12
|
+
# 2. 在 lib/linguist/languages.yml 里按字母序插入下面这段
|
|
13
|
+
# 3. 在 samples/ 下放样例目录:samples/co/*.co(至少一个完整程序)
|
|
14
|
+
# 4. 跑 bundle exec rake build 校验,再跑 bundle exec rake test
|
|
15
|
+
# 5. 提 PR,说明「co 是用 co 自身编写的编程语言」
|
|
16
|
+
#
|
|
17
|
+
# .co 扩展名的归属:Linguist 里 Pascal 原本占着 .co。
|
|
18
|
+
# Linguist 支持 overrides,同一扩展名可以由多个语言声明,
|
|
19
|
+
# 由各平台自行选择(GitHub 会用「主要语言」优先级最高的那个)。
|
|
20
|
+
# 所以这里给 co 加 overrides 抢 .co,同时建议 Pascal 改用 .p/.pas。
|
|
21
|
+
# ============================================================
|
|
22
|
+
co:
|
|
23
|
+
type: programming
|
|
24
|
+
color: "#8f5bd7"
|
|
25
|
+
aliases:
|
|
26
|
+
- "co语言"
|
|
27
|
+
extensions:
|
|
28
|
+
- ".co"
|
|
29
|
+
# .co 原属 Pascal;Linguist 的 overrides 允许同一扩展名被多语言声明,
|
|
30
|
+
# 由平台按主要语言优先级挑。Pascal 建议同时改用 .p / .pas。
|
|
31
|
+
overrides:
|
|
32
|
+
- ".co"
|
|
33
|
+
tm_scope: source.co
|
|
34
|
+
ace_mode: text
|
|
35
|
+
codemirror_mode: co
|
|
36
|
+
codemirror_mime_type: text/x-co
|
|
37
|
+
language_id: 0
|
|
38
|
+
# 与语言同名的文件夹(samples/co)会被 Linguist 认作该语言的样例
|
|
39
|
+
interpreters: []
|
|
40
|
+
filenames: []
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { parseSource } = require('./parse.js');
|
|
4
|
+
|
|
5
|
+
function lintSource(source, filename = '') {
|
|
6
|
+
try {
|
|
7
|
+
const ast = parseSource(source);
|
|
8
|
+
return { ok: true, diagnostics: [], ast };
|
|
9
|
+
} catch (error) {
|
|
10
|
+
return {
|
|
11
|
+
ok: false,
|
|
12
|
+
diagnostics: [{ file: filename, level: 'error', message: error.message }],
|
|
13
|
+
ast: null
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = { lintSource };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const http = require('http');
|
|
4
|
+
const { metadata, identify } = require('./identify.js');
|
|
5
|
+
const { parseSource } = require('./parse.js');
|
|
6
|
+
const { lintSource } = require('./lint.js');
|
|
7
|
+
|
|
8
|
+
const 最大请求体 = 2 * 1024 * 1024;
|
|
9
|
+
|
|
10
|
+
function sendJson(res, status, body) {
|
|
11
|
+
if (res.headersSent) return res.end();
|
|
12
|
+
const text = JSON.stringify(body, null, 2);
|
|
13
|
+
res.writeHead(status, {
|
|
14
|
+
'Content-Type': 'application/json; charset=utf-8',
|
|
15
|
+
'X-Content-Type-Options': 'nosniff',
|
|
16
|
+
'Cache-Control': 'no-store'
|
|
17
|
+
});
|
|
18
|
+
res.end(text);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function readJsonBody(req) {
|
|
22
|
+
return new Promise((resolve, reject) => {
|
|
23
|
+
const chunks = [];
|
|
24
|
+
let size = 0;
|
|
25
|
+
let settled = false;
|
|
26
|
+
const fail = (statusCode, message) => {
|
|
27
|
+
if (settled) return;
|
|
28
|
+
settled = true;
|
|
29
|
+
const error = new Error(message);
|
|
30
|
+
error.statusCode = statusCode;
|
|
31
|
+
reject(error);
|
|
32
|
+
};
|
|
33
|
+
req.on('data', (chunk) => {
|
|
34
|
+
if (settled) return;
|
|
35
|
+
size += chunk.length;
|
|
36
|
+
if (size > 最大请求体) {
|
|
37
|
+
fail(413, '请求体超过 2MB 上限');
|
|
38
|
+
// 不要立刻 destroy:先排空剩余请求体,让客户端能收到 413 JSON,
|
|
39
|
+
// 而不是只看到 ECONNRESET / curl 的 empty reply。
|
|
40
|
+
req.resume();
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
chunks.push(chunk);
|
|
44
|
+
});
|
|
45
|
+
req.on('end', () => {
|
|
46
|
+
if (settled) return;
|
|
47
|
+
const body = Buffer.concat(chunks).toString('utf8');
|
|
48
|
+
if (!body) return resolve({});
|
|
49
|
+
try {
|
|
50
|
+
const parsed = JSON.parse(body);
|
|
51
|
+
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
|
52
|
+
return fail(400, 'JSON 顶层必须是对象');
|
|
53
|
+
}
|
|
54
|
+
resolve(parsed);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
fail(400, 'JSON 无效:' + error.message);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
req.on('error', (error) => fail(error.statusCode || 400, error.message));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function 取源码(input) {
|
|
64
|
+
if (input.source == null) return '';
|
|
65
|
+
if (typeof input.source !== 'string') {
|
|
66
|
+
const error = new Error('source 必须是字符串');
|
|
67
|
+
error.statusCode = 400;
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
return input.source;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const server = http.createServer(async (req, res) => {
|
|
74
|
+
try {
|
|
75
|
+
if (req.method === 'GET' && req.url === '/metadata') {
|
|
76
|
+
return sendJson(res, 200, metadata);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (req.method === 'POST') {
|
|
80
|
+
const input = await readJsonBody(req);
|
|
81
|
+
|
|
82
|
+
if (req.url === '/identify') {
|
|
83
|
+
return sendJson(res, 200, identify(input, { readFile: false }));
|
|
84
|
+
}
|
|
85
|
+
if (req.url === '/parse') {
|
|
86
|
+
try {
|
|
87
|
+
return sendJson(res, 200, { ast: parseSource(取源码(input)) });
|
|
88
|
+
} catch (error) {
|
|
89
|
+
error.statusCode = error.statusCode || 400;
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
if (req.url === '/lint') {
|
|
94
|
+
const filename = typeof input.filename === 'string' ? input.filename.slice(0, 4096) : '';
|
|
95
|
+
return sendJson(res, 200, lintSource(取源码(input), filename));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
sendJson(res, 404, { error: 'not found' });
|
|
100
|
+
} catch (error) {
|
|
101
|
+
const status = error.statusCode || 500;
|
|
102
|
+
// 解析失败是用户输入问题,不应伪装成服务端 500;也不回传堆栈。
|
|
103
|
+
const message = status === 500 ? '服务器内部错误' : error.message;
|
|
104
|
+
if (status === 500) console.error('[co language API]', error && error.message);
|
|
105
|
+
sendJson(res, status, { error: message });
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
if (require.main === module) {
|
|
110
|
+
const port = Number(process.env.PORT || 3000);
|
|
111
|
+
const host = process.env.HOST || '127.0.0.1';
|
|
112
|
+
server.listen(port, host, () => {
|
|
113
|
+
console.log(`co language API listening on http://${host}:${port}`);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
module.exports = { server };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yeyelailou/co-linux-arm64",
|
|
3
|
+
"version": "0.0.11",
|
|
4
|
+
"description": "cocn 引导二进制(linux-arm64 平台)—— 供 @yeyelailou/co 通过 optionalDependencies 引用",
|
|
5
|
+
"os": [
|
|
6
|
+
"linux",
|
|
7
|
+
"android"
|
|
8
|
+
],
|
|
9
|
+
"cpu": [
|
|
10
|
+
"arm64"
|
|
11
|
+
],
|
|
12
|
+
"license": "Apache-2.0",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://gitee.com/COSMOnb666/cocn.git"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://gitee.com/COSMOnb666/cocn",
|
|
18
|
+
"author": "张宇 (cosmo)",
|
|
19
|
+
"keywords": [
|
|
20
|
+
"coCN",
|
|
21
|
+
"中文编程",
|
|
22
|
+
"引导器",
|
|
23
|
+
"bootstrap",
|
|
24
|
+
"linux-arm64"
|
|
25
|
+
],
|
|
26
|
+
"files": [
|
|
27
|
+
"bin/"
|
|
28
|
+
]
|
|
29
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yeyelailou/co-linux-x64",
|
|
3
|
+
"version": "0.0.11",
|
|
4
|
+
"description": "cocn 引导二进制(linux-x64 平台)—— 供 @yeyelailou/co 通过 optionalDependencies 引用",
|
|
5
|
+
"os": [
|
|
6
|
+
"linux"
|
|
7
|
+
],
|
|
8
|
+
"cpu": [
|
|
9
|
+
"x64"
|
|
10
|
+
],
|
|
11
|
+
"license": "Apache-2.0",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://gitee.com/COSMOnb666/cocn.git"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://gitee.com/COSMOnb666/cocn",
|
|
17
|
+
"author": "张宇 (cosmo)",
|
|
18
|
+
"keywords": [
|
|
19
|
+
"coCN",
|
|
20
|
+
"中文编程",
|
|
21
|
+
"引导器",
|
|
22
|
+
"bootstrap",
|
|
23
|
+
"linux-x64"
|
|
24
|
+
],
|
|
25
|
+
"files": [
|
|
26
|
+
"bin/"
|
|
27
|
+
]
|
|
28
|
+
}
|