@slopdam/core 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 +21 -0
- package/README.ko.md +117 -0
- package/README.md +116 -2
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +11 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/pino.d.ts +26 -0
- package/dist/cjs/pino.js +49 -0
- package/dist/cjs/pyregex.d.ts +28 -0
- package/dist/cjs/pyregex.js +184 -0
- package/dist/cjs/sanitizer.d.ts +46 -0
- package/dist/cjs/sanitizer.js +265 -0
- package/dist/cjs/spec.generated.d.ts +2 -0
- package/dist/cjs/spec.generated.js +468 -0
- package/dist/cjs/types.d.ts +45 -0
- package/dist/cjs/types.js +4 -0
- package/dist/cjs/winston.d.ts +31 -0
- package/dist/cjs/winston.js +41 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/pino.d.ts +26 -0
- package/dist/esm/pino.js +46 -0
- package/dist/esm/pyregex.d.ts +28 -0
- package/dist/esm/pyregex.js +173 -0
- package/dist/esm/sanitizer.d.ts +46 -0
- package/dist/esm/sanitizer.js +260 -0
- package/dist/esm/spec.generated.d.ts +2 -0
- package/dist/esm/spec.generated.js +465 -0
- package/dist/esm/types.d.ts +45 -0
- package/dist/esm/types.js +3 -0
- package/dist/esm/winston.d.ts +31 -0
- package/dist/esm/winston.js +38 -0
- package/package.json +79 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 The slopdam Authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.ko.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
<h1 align="center">JavaScript용 slopdam</h1>
|
|
2
|
+
|
|
3
|
+
<p align="center"><i><a href="https://github.com/slopdam/slopdam">slopdam</a>의 JavaScript, TypeScript 구현: 긴 대시, 이모지, 구분선, 마크다운이 로그에 남지 않게 합니다.</i></p>
|
|
4
|
+
|
|
5
|
+
<p align="center"><a href="https://github.com/slopdam/slopdam/blob/main/js/README.md">English</a> | <b>한국어</b></p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://github.com/slopdam/slopdam/actions/workflows/ci.yml"><img src="https://github.com/slopdam/slopdam/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
|
|
9
|
+
<a href="https://github.com/slopdam/slopdam/blob/main/spec/README.ko.md"><img src="https://img.shields.io/badge/spec-v1%20conformance%20tested-informational.svg" alt="명세 v1: 적합성 테스트"></a>
|
|
10
|
+
<a href="https://github.com/slopdam/slopdam/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="라이선스: MIT"></a>
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
`@slopdam/core`는 AI 코딩 도구가 로그 메시지와 콘솔 출력에 남기는 흔적을 걷어냅니다. 긴 대시와 그와 닮은 선(한글 `ㅡ` 포함), 장식 이모지, 구분선, 마크다운 굵은 글씨, 둥근 따옴표, 보이지 않는 문자가 대상입니다. Python 패키지와 같은 [적합성 사례](https://github.com/slopdam/slopdam/blob/main/spec/README.ko.md)로 테스트하므로 결과가 똑같습니다.
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
입력 : ✅ **저장 완료** ㅡ 2024–2025년 데이터 3건 처리했습니다! 🎉
|
|
17
|
+
reader: 저장 완료: 2024~2025년 데이터 3건 처리했습니다.
|
|
18
|
+
dev : [OK] 저장 완료 - 2024-2025년 데이터 3건 처리했습니다!
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
- `reader`(기본): 사람이 읽는 글. CLI 메시지, 알림, 보고서에 씁니다.
|
|
22
|
+
- `dev`: 검색하기 쉬운 글. 로그 파일과 수집기에 씁니다. 상태 이모지는 지우지 않고 `[OK]`, `[FAIL]`, `[WARN]`으로 바꿉니다.
|
|
23
|
+
|
|
24
|
+
## 설치
|
|
25
|
+
|
|
26
|
+
Node.js 20 이상 또는 최신 브라우저에서 동작합니다. 실행 의존성이 없고, ESM과 CommonJS 빌드에 TypeScript 타입이 들어 있습니다.
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install @slopdam/core
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 사용법
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import { clean, Sanitizer } from "@slopdam/core";
|
|
36
|
+
|
|
37
|
+
clean("✅ **저장 완료** ㅡ 3건 처리했습니다! 🎉"); // "저장 완료: 3건 처리했습니다."
|
|
38
|
+
clean("✅ **Saved** — 3 rows 🎉"); // "Saved: 3 rows"
|
|
39
|
+
|
|
40
|
+
const dev = new Sanitizer({ profile: "dev" });
|
|
41
|
+
dev.clean("✅ 업로드 완료 ㅡ 3개 파일"); // "[OK] 업로드 완료 - 3개 파일"
|
|
42
|
+
dev.clean("❌ 업로드 실패", "error"); // "업로드 실패" (로그 레벨이 이미 실패를 말함)
|
|
43
|
+
|
|
44
|
+
console.log(dev.findings("log.info('🚀 시작 ㅡ 정상')"));
|
|
45
|
+
// [ { ruleId: 'emoji.pictograph', text: '🚀', fix: '', ... },
|
|
46
|
+
// { ruleId: 'dash.run', text: 'ㅡ', fix: ' - ', ... } ]
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
CommonJS에서도 `require("@slopdam/core")`로 똑같이 씁니다.
|
|
50
|
+
|
|
51
|
+
### pino
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
import pino from "pino";
|
|
55
|
+
import { slopdamPino } from "@slopdam/core/pino";
|
|
56
|
+
|
|
57
|
+
const log = pino({ ...slopdamPino({ profile: "dev" }) });
|
|
58
|
+
log.info("✅ **저장 완료** %d건 ㅡ 처리했습니다!", 3); // msg: "[OK] 저장 완료 3건 - 처리했습니다!"
|
|
59
|
+
log.error(err, "❌ 업로드 실패"); // msg: "업로드 실패", err는 그대로
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
pino가 쓰기 전에 모든 호출의 메시지를 정리합니다. 객체와 오류는 그대로 두고, 구분선만 출력하던 호출은 버립니다. 프로필은 로거 전체에 적용되므로, `reader`와 `dev`를 다른 곳에 보내려면 로거를 두 개 만드세요. 이미 `hooks.logMethod`를 쓰고 있다면 `slopdamPino({ hooks })`로 넘기면 둘 다 실행됩니다.
|
|
63
|
+
|
|
64
|
+
### winston
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
import winston from "winston";
|
|
68
|
+
import { slopdamWinston } from "@slopdam/core/winston";
|
|
69
|
+
|
|
70
|
+
const { combine, printf, json } = winston.format;
|
|
71
|
+
winston.createLogger({
|
|
72
|
+
transports: [
|
|
73
|
+
new winston.transports.Console({ format: combine(slopdamWinston(), printf((i) => i.message)) }),
|
|
74
|
+
new winston.transports.File({ filename: "app.log", format: combine(slopdamWinston({ profile: "dev" }), json()) }),
|
|
75
|
+
],
|
|
76
|
+
});
|
|
77
|
+
// 화면: "저장 완료: 3건 처리했습니다." app.log: {"message":"[OK] 저장 완료 - 3건 처리했습니다!", ...}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
winston은 트랜스포트마다 로그 항목의 사본을 넘기므로 트랜스포트마다 다른 프로필을 쓸 수 있습니다. `slopdamWinston()`은 `format.splat()` 뒤, `colorize()`, `printf()`, `simple()`, `json()` 앞에 둡니다. 스택 트레이스는 그대로 둡니다.
|
|
81
|
+
|
|
82
|
+
두 연동 모두 pino나 winston을 불러오지 않으므로 `@slopdam/core`는 여전히 실행 의존성이 없습니다.
|
|
83
|
+
|
|
84
|
+
### 옵션
|
|
85
|
+
|
|
86
|
+
| 옵션 | 값 | 기본값 |
|
|
87
|
+
|---|---|---|
|
|
88
|
+
| `profile` | `"reader"`, `"dev"` | `"reader"` |
|
|
89
|
+
| `level` | `"basic"`, `"strong"`(군더더기 표현도 제거), `"strict"`(ASCII가 아닌 기호를 모두 알림) | `"basic"` |
|
|
90
|
+
| `disable` | 끌 규칙 ID나 군. 예: `["emoji"]`, `["dash.run"]` | `[]` |
|
|
91
|
+
| `dashInline` | `reader`에서 문장 중간 대시를 바꿀 문자: `"colon"`, `"comma"`, `"hyphen"`, `"space"` | `"colon"` |
|
|
92
|
+
|
|
93
|
+
`clean()`의 두 번째 인자는 로그 레벨입니다. 이름(`"error"`, `"warning"`)이나 Python logging 숫자(40, 30)로 줍니다. `findings()`는 `clean()`이 바꿀 부분을 돌려주며, `start`와 `end`는 입력 안의 UTF-16 위치입니다.
|
|
94
|
+
|
|
95
|
+
URL, 인라인 코드, `key=value`의 값, 파일 경로는 바꾸지 않습니다. 두 번 정리해도 한 번 정리한 결과와 같습니다.
|
|
96
|
+
|
|
97
|
+
## Python과 같은 결과를 내는 방법
|
|
98
|
+
|
|
99
|
+
규칙을 손으로 다시 쓰지 않습니다. 빌드할 때 저장소의 [spec/](https://github.com/slopdam/slopdam/blob/main/spec/README.ko.md)을 직접 읽고, 그 안의 패턴은 패키지를 불러올 때 Python 정규식 문법에서 변환됩니다(유니코드 `\w`, `\s`, `\b`와 Python의 `^`, `$`). 문맥에 따라 바꾸는 동작 네 가지만 TypeScript로 작성했습니다. PR마다 Python 패키지와 이 패키지의 ESM, CommonJS 빌드에 같은 적합성 사례를 실행하므로 한 언어에서만 규칙이 바뀔 수 없습니다. 무작위 입력 20,000개에서 두 패키지의 결과가 같았습니다.
|
|
100
|
+
|
|
101
|
+
## 로드맵
|
|
102
|
+
|
|
103
|
+
slopdam 0.5 마일스톤의 일부입니다([로드맵](https://github.com/slopdam/slopdam/blob/main/ROADMAP.ko.md)). 핵심 패키지와 pino, winston 연동에 이어 `eslint-plugin-slopdam`을 만듭니다.
|
|
104
|
+
|
|
105
|
+
## 기여
|
|
106
|
+
|
|
107
|
+
규칙 변경은 Python 기준 구현과 `spec/`에서 적합성 사례와 함께 한 번만 합니다. 이 패키지는 다음 빌드에서 이를 반영하고, 같은 PR에서 통과 여부가 드러납니다. 기여는 slopdam의 [기여 안내](https://github.com/slopdam/slopdam/blob/main/CONTRIBUTING.ko.md)(DCO 서명), [행동 강령](https://github.com/slopdam/slopdam/blob/main/CODE_OF_CONDUCT.ko.md), [보안 정책](https://github.com/slopdam/slopdam/blob/main/SECURITY.ko.md)을 따릅니다.
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
cd js
|
|
111
|
+
npm install
|
|
112
|
+
npm test # 빌드한 뒤 적합성 사례와 API 테스트를 실행
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## 라이선스
|
|
116
|
+
|
|
117
|
+
[MIT](https://github.com/slopdam/slopdam/blob/main/LICENSE)
|
package/README.md
CHANGED
|
@@ -1,3 +1,117 @@
|
|
|
1
|
-
|
|
1
|
+
<h1 align="center">slopdam for JavaScript</h1>
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p align="center"><i>The JavaScript and TypeScript port of <a href="https://github.com/slopdam/slopdam">slopdam</a>: em dashes, emoji, separator lines and markdown stay out of your logs.</i></p>
|
|
4
|
+
|
|
5
|
+
<p align="center"><b>English</b> | <a href="https://github.com/slopdam/slopdam/blob/main/js/README.ko.md">한국어</a></p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<a href="https://github.com/slopdam/slopdam/actions/workflows/ci.yml"><img src="https://github.com/slopdam/slopdam/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
|
|
9
|
+
<a href="https://github.com/slopdam/slopdam/blob/main/spec/README.md"><img src="https://img.shields.io/badge/spec-v1%20conformance%20tested-informational.svg" alt="Spec v1: conformance tested"></a>
|
|
10
|
+
<a href="https://github.com/slopdam/slopdam/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License: MIT"></a>
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
`@slopdam/core` removes the traces AI coding tools leave in log messages and console output: em dashes and look-alike lines (including the Korean `ㅡ`), decorative emoji, separator lines, markdown bold, curly quotes and invisible characters. It gives exactly the same output as the Python package, because both are tested against the same [conformance cases](https://github.com/slopdam/slopdam/blob/main/spec/README.md).
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
input : ✅ **Saved** — 3 rows processed 🎉
|
|
17
|
+
reader: Saved: 3 rows processed
|
|
18
|
+
dev : [OK] Saved - 3 rows processed
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
- `reader` (default): plain text for people, such as CLI messages, alerts and reports.
|
|
22
|
+
- `dev`: grep-friendly text for log files and collectors. Status emoji become `[OK]`, `[FAIL]` and `[WARN]` instead of disappearing.
|
|
23
|
+
|
|
24
|
+
## Install
|
|
25
|
+
|
|
26
|
+
Node.js 20 or later, or any modern browser. No runtime dependencies; ESM and CommonJS builds with TypeScript types.
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install @slopdam/core
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Use
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import { clean, Sanitizer } from "@slopdam/core";
|
|
36
|
+
|
|
37
|
+
clean("✅ **Saved** — 3 rows 🎉"); // "Saved: 3 rows"
|
|
38
|
+
clean("✅ **저장 완료** ㅡ 3건 처리했습니다! 🎉"); // "저장 완료: 3건 처리했습니다."
|
|
39
|
+
|
|
40
|
+
const dev = new Sanitizer({ profile: "dev" });
|
|
41
|
+
dev.clean("✅ Upload done — 3 files"); // "[OK] Upload done - 3 files"
|
|
42
|
+
dev.clean("❌ Upload failed", "error"); // "Upload failed" (the level already says it)
|
|
43
|
+
|
|
44
|
+
console.log(dev.findings("log.info('🚀 Started — ok')"));
|
|
45
|
+
// [ { ruleId: 'emoji.pictograph', text: '🚀', fix: '', start: 10, end: 13, ... },
|
|
46
|
+
// { ruleId: 'dash.run', text: '—', fix: ' - ', ... } ]
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`require("@slopdam/core")` works the same way in CommonJS.
|
|
50
|
+
|
|
51
|
+
### pino
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
import pino from "pino";
|
|
55
|
+
import { slopdamPino } from "@slopdam/core/pino";
|
|
56
|
+
|
|
57
|
+
const log = pino({ ...slopdamPino({ profile: "dev" }) });
|
|
58
|
+
log.info("✅ **Saved** %d rows — done!", 3); // msg: "[OK] Saved 3 rows - done!"
|
|
59
|
+
log.error(err, "❌ Upload failed"); // msg: "Upload failed", err untouched
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The message of every call is cleaned before pino writes it. Objects and errors are left as they are, and a call that only printed a separator line is dropped. The profile applies to the whole logger; to send `reader` text to one place and `dev` text to another, use two loggers. If you already pass `hooks.logMethod`, give it to `slopdamPino({ hooks })` so both run.
|
|
63
|
+
|
|
64
|
+
### winston
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
import winston from "winston";
|
|
68
|
+
import { slopdamWinston } from "@slopdam/core/winston";
|
|
69
|
+
|
|
70
|
+
const { combine, printf, json } = winston.format;
|
|
71
|
+
winston.createLogger({
|
|
72
|
+
transports: [
|
|
73
|
+
new winston.transports.Console({ format: combine(slopdamWinston(), printf((i) => i.message)) }),
|
|
74
|
+
new winston.transports.File({ filename: "app.log", format: combine(slopdamWinston({ profile: "dev" }), json()) }),
|
|
75
|
+
],
|
|
76
|
+
});
|
|
77
|
+
// console: "Saved 3 rows: done!" app.log: {"message":"[OK] Saved 3 rows - done!", ...}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
winston gives every transport its own copy of the entry, so each one can use its own profile. Put `slopdamWinston()` after `format.splat()` and before `colorize()`, `printf()`, `simple()` or `json()`. Stack traces are kept as they are.
|
|
81
|
+
|
|
82
|
+
Neither integration imports pino or winston, so `@slopdam/core` still has no runtime dependencies.
|
|
83
|
+
|
|
84
|
+
### Options
|
|
85
|
+
|
|
86
|
+
| Option | Values | Default |
|
|
87
|
+
|---|---|---|
|
|
88
|
+
| `profile` | `"reader"`, `"dev"` | `"reader"` |
|
|
89
|
+
| `level` | `"basic"`, `"strong"` (also removes filler words), `"strict"` (also reports any non-ASCII symbol) | `"basic"` |
|
|
90
|
+
| `disable` | rule IDs or groups, such as `["emoji"]` or `["dash.run"]` | `[]` |
|
|
91
|
+
| `dashInline` | what an inline dash becomes in `reader`: `"colon"`, `"comma"`, `"hyphen"`, `"space"` | `"colon"` |
|
|
92
|
+
|
|
93
|
+
The second argument of `clean()` is the log level, as a name (`"error"`, `"warning"`) or a Python logging number (40, 30). `findings()` returns what `clean()` would change, with `start` and `end` as UTF-16 offsets into the input.
|
|
94
|
+
|
|
95
|
+
URLs, inline code, `key=value` values and file paths are never changed. Cleaning twice gives the same result as cleaning once.
|
|
96
|
+
|
|
97
|
+
## How it stays in step with Python
|
|
98
|
+
|
|
99
|
+
The rules are not rewritten by hand. The build reads the repository's [spec/](https://github.com/slopdam/slopdam/blob/main/spec/README.md) directly; its patterns are translated from Python's regular expression dialect when the package loads (Unicode `\w`, `\s` and `\b`, and Python's `^` and `$`), and only the four context-dependent actions are written in TypeScript. Every pull request runs the conformance cases against the Python package and against this package's ESM and CommonJS builds, so a rule cannot change in one language only. On 20,000 random inputs the two packages gave identical results.
|
|
100
|
+
|
|
101
|
+
## Roadmap
|
|
102
|
+
|
|
103
|
+
Part of slopdam milestone 0.5 ([roadmap](https://github.com/slopdam/slopdam/blob/main/ROADMAP.md)): the core package and the pino and winston integrations, then `eslint-plugin-slopdam`.
|
|
104
|
+
|
|
105
|
+
## Contributing
|
|
106
|
+
|
|
107
|
+
A rule change is made once, in the Python reference and `spec/`, with conformance cases; this package picks it up on the next build, and the same pull request shows whether it passes. Contributions follow the slopdam [contribution guide](https://github.com/slopdam/slopdam/blob/main/CONTRIBUTING.md) (DCO sign-off), [code of conduct](https://github.com/slopdam/slopdam/blob/main/CODE_OF_CONDUCT.md) and [security policy](https://github.com/slopdam/slopdam/blob/main/SECURITY.md).
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
cd js
|
|
111
|
+
npm install
|
|
112
|
+
npm test # builds, then runs the conformance suite and API tests
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## License
|
|
116
|
+
|
|
117
|
+
[MIT](https://github.com/slopdam/slopdam/blob/main/LICENSE)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SPEC_VERSION = exports.RULE_IDS = exports.PROFILES = exports.LEVELS = exports.clean = exports.Sanitizer = void 0;
|
|
4
|
+
// SPDX-License-Identifier: MIT
|
|
5
|
+
var sanitizer_js_1 = require("./sanitizer.js");
|
|
6
|
+
Object.defineProperty(exports, "Sanitizer", { enumerable: true, get: function () { return sanitizer_js_1.Sanitizer; } });
|
|
7
|
+
Object.defineProperty(exports, "clean", { enumerable: true, get: function () { return sanitizer_js_1.clean; } });
|
|
8
|
+
Object.defineProperty(exports, "LEVELS", { enumerable: true, get: function () { return sanitizer_js_1.LEVELS; } });
|
|
9
|
+
Object.defineProperty(exports, "PROFILES", { enumerable: true, get: function () { return sanitizer_js_1.PROFILES; } });
|
|
10
|
+
Object.defineProperty(exports, "RULE_IDS", { enumerable: true, get: function () { return sanitizer_js_1.RULE_IDS; } });
|
|
11
|
+
Object.defineProperty(exports, "SPEC_VERSION", { enumerable: true, get: function () { return sanitizer_js_1.SPEC_VERSION; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pino integration. Spread the options into pino():
|
|
3
|
+
*
|
|
4
|
+
* import pino from "pino";
|
|
5
|
+
* import { slopdamPino } from "@slopdam/core/pino";
|
|
6
|
+
* const log = pino({ ...slopdamPino({ profile: "dev" }) });
|
|
7
|
+
*
|
|
8
|
+
* The message string of every log call is cleaned before pino serializes it. A call whose
|
|
9
|
+
* message was only a separator line, with no object or error, is dropped. Objects and
|
|
10
|
+
* errors are never changed. pino is not imported here.
|
|
11
|
+
*/
|
|
12
|
+
import { type SanitizerOptions } from "./sanitizer.js";
|
|
13
|
+
export interface SlopdamPinoOptions extends SanitizerOptions {
|
|
14
|
+
/** Merge with hooks you already pass to pino. */
|
|
15
|
+
hooks?: {
|
|
16
|
+
logMethod?: LogMethodHook;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
type LogFn = (...args: unknown[]) => void;
|
|
20
|
+
type LogMethodHook = (this: unknown, args: unknown[], method: LogFn, level: number) => void;
|
|
21
|
+
export declare function slopdamPino(options?: SlopdamPinoOptions): {
|
|
22
|
+
hooks: {
|
|
23
|
+
logMethod: LogMethodHook;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
export {};
|
package/dist/cjs/pino.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.slopdamPino = slopdamPino;
|
|
4
|
+
// SPDX-License-Identifier: MIT
|
|
5
|
+
/**
|
|
6
|
+
* pino integration. Spread the options into pino():
|
|
7
|
+
*
|
|
8
|
+
* import pino from "pino";
|
|
9
|
+
* import { slopdamPino } from "@slopdam/core/pino";
|
|
10
|
+
* const log = pino({ ...slopdamPino({ profile: "dev" }) });
|
|
11
|
+
*
|
|
12
|
+
* The message string of every log call is cleaned before pino serializes it. A call whose
|
|
13
|
+
* message was only a separator line, with no object or error, is dropped. Objects and
|
|
14
|
+
* errors are never changed. pino is not imported here.
|
|
15
|
+
*/
|
|
16
|
+
const sanitizer_js_1 = require("./sanitizer.js");
|
|
17
|
+
/** pino's numbers (10 trace ... 60 fatal) as the level names slopdam understands. */
|
|
18
|
+
function levelName(level) {
|
|
19
|
+
if (level >= 60)
|
|
20
|
+
return "critical";
|
|
21
|
+
if (level >= 50)
|
|
22
|
+
return "error";
|
|
23
|
+
if (level >= 40)
|
|
24
|
+
return "warning";
|
|
25
|
+
if (level >= 30)
|
|
26
|
+
return "info";
|
|
27
|
+
return "debug";
|
|
28
|
+
}
|
|
29
|
+
function slopdamPino(options = {}) {
|
|
30
|
+
const { hooks, ...sanitizerOptions } = options;
|
|
31
|
+
const sanitizer = new sanitizer_js_1.Sanitizer(sanitizerOptions);
|
|
32
|
+
const next = hooks?.logMethod;
|
|
33
|
+
function logMethod(args, method, level) {
|
|
34
|
+
// log([mergingObject], [message], [...interpolationValues])
|
|
35
|
+
const at = typeof args[0] === "string" ? 0 : typeof args[1] === "string" ? 1 : -1;
|
|
36
|
+
if (at >= 0) {
|
|
37
|
+
const cleaned = sanitizer.clean(args[at], levelName(level));
|
|
38
|
+
if (!cleaned && at === 0 && args.length === 1)
|
|
39
|
+
return; // only a separator line
|
|
40
|
+
args = [...args];
|
|
41
|
+
args[at] = cleaned;
|
|
42
|
+
}
|
|
43
|
+
if (next)
|
|
44
|
+
next.call(this, args, method, level);
|
|
45
|
+
else
|
|
46
|
+
method.apply(this, args);
|
|
47
|
+
}
|
|
48
|
+
return { hooks: { logMethod } };
|
|
49
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Translate the Python `re` patterns in the spec into JavaScript regular expressions
|
|
3
|
+
* with the `u` flag, keeping Python's meaning:
|
|
4
|
+
*
|
|
5
|
+
* \w \s \d \b Unicode-aware, as in Python 3 (JavaScript's are ASCII-only)
|
|
6
|
+
* \A \Z start and end of the whole string
|
|
7
|
+
* \UXXXXXXXX \u{X}
|
|
8
|
+
* . any character except \n (JavaScript also stops at \r, \u2028, \u2029)
|
|
9
|
+
* ^ $ Python's anchors; with the multiline flag only \n separates lines (in
|
|
10
|
+
* JavaScript's m mode \r, \u2028 and \u2029 do too), so no JS flag is used
|
|
11
|
+
* (?P<name> (?<name>
|
|
12
|
+
*
|
|
13
|
+
* Python's character classes, measured on its Unicode database: \w is letters, numbers and
|
|
14
|
+
* "_" (categories L*, N*); \s is the set below; str.isdigit() is Nd plus PY_DIGIT_EXTRA.
|
|
15
|
+
*/
|
|
16
|
+
export declare const PY_WORD = "\\p{L}\\p{N}_";
|
|
17
|
+
export declare const PY_SPACE = "\\t-\\r\\x1c-\\x20\\x85\\xa0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000";
|
|
18
|
+
export declare const isAlnum: (c: string) => boolean;
|
|
19
|
+
export declare const isDigit: (c: string) => boolean;
|
|
20
|
+
export declare const isSpace: (c: string) => boolean;
|
|
21
|
+
/** Python's str.strip() / str.rstrip() with no argument. */
|
|
22
|
+
export declare function pyStrip(s: string): string;
|
|
23
|
+
export declare function pyRstrip(s: string): string;
|
|
24
|
+
/** Python's str.splitlines(). */
|
|
25
|
+
export declare function splitlines(s: string): string[];
|
|
26
|
+
export declare function translate(src: string, multiline?: boolean): string;
|
|
27
|
+
/** A global, Unicode JavaScript RegExp for a spec pattern and its flags. */
|
|
28
|
+
export declare function compile(pattern: string, flags?: readonly string[]): RegExp;
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
/**
|
|
4
|
+
* Translate the Python `re` patterns in the spec into JavaScript regular expressions
|
|
5
|
+
* with the `u` flag, keeping Python's meaning:
|
|
6
|
+
*
|
|
7
|
+
* \w \s \d \b Unicode-aware, as in Python 3 (JavaScript's are ASCII-only)
|
|
8
|
+
* \A \Z start and end of the whole string
|
|
9
|
+
* \UXXXXXXXX \u{X}
|
|
10
|
+
* . any character except \n (JavaScript also stops at \r, \u2028, \u2029)
|
|
11
|
+
* ^ $ Python's anchors; with the multiline flag only \n separates lines (in
|
|
12
|
+
* JavaScript's m mode \r, \u2028 and \u2029 do too), so no JS flag is used
|
|
13
|
+
* (?P<name> (?<name>
|
|
14
|
+
*
|
|
15
|
+
* Python's character classes, measured on its Unicode database: \w is letters, numbers and
|
|
16
|
+
* "_" (categories L*, N*); \s is the set below; str.isdigit() is Nd plus PY_DIGIT_EXTRA.
|
|
17
|
+
*/
|
|
18
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
+
exports.isSpace = exports.isDigit = exports.isAlnum = exports.PY_SPACE = exports.PY_WORD = void 0;
|
|
20
|
+
exports.pyStrip = pyStrip;
|
|
21
|
+
exports.pyRstrip = pyRstrip;
|
|
22
|
+
exports.splitlines = splitlines;
|
|
23
|
+
exports.translate = translate;
|
|
24
|
+
exports.compile = compile;
|
|
25
|
+
exports.PY_WORD = "\\p{L}\\p{N}_";
|
|
26
|
+
exports.PY_SPACE = "\\t-\\r\\x1c-\\x20\\x85\\xa0\\u1680\\u2000-\\u200a\\u2028\\u2029\\u202f\\u205f\\u3000";
|
|
27
|
+
const PY_DIGIT_EXTRA = "\\u{B2}-\\u{B3}\\u{B9}\\u{1369}-\\u{1371}\\u{19DA}\\u{2070}\\u{2074}-\\u{2079}\\u{2080}-\\u{2089}" +
|
|
28
|
+
"\\u{2460}-\\u{2468}\\u{2474}-\\u{247C}\\u{2488}-\\u{2490}\\u{24EA}\\u{24F5}-\\u{24FD}\\u{24FF}" +
|
|
29
|
+
"\\u{2776}-\\u{277E}\\u{2780}-\\u{2788}\\u{278A}-\\u{2792}\\u{10A40}-\\u{10A43}\\u{10E60}-\\u{10E68}" +
|
|
30
|
+
"\\u{11052}-\\u{1105A}\\u{1F100}-\\u{1F10A}";
|
|
31
|
+
const isAlnum = (c) => /^[\p{L}\p{N}]$/u.test(c);
|
|
32
|
+
exports.isAlnum = isAlnum;
|
|
33
|
+
const DIGIT_RE = new RegExp(`^[\\p{Nd}${PY_DIGIT_EXTRA}]$`, "u");
|
|
34
|
+
const isDigit = (c) => DIGIT_RE.test(c);
|
|
35
|
+
exports.isDigit = isDigit;
|
|
36
|
+
const SPACE_RE = new RegExp(`[${exports.PY_SPACE}]`, "u");
|
|
37
|
+
const isSpace = (c) => SPACE_RE.test(c);
|
|
38
|
+
exports.isSpace = isSpace;
|
|
39
|
+
/** Python's str.strip() / str.rstrip() with no argument. */
|
|
40
|
+
function pyStrip(s) {
|
|
41
|
+
return s.replace(new RegExp(`^[${exports.PY_SPACE}]+|[${exports.PY_SPACE}]+$`, "gu"), "");
|
|
42
|
+
}
|
|
43
|
+
function pyRstrip(s) {
|
|
44
|
+
return s.replace(new RegExp(`[${exports.PY_SPACE}]+$`, "u"), "");
|
|
45
|
+
}
|
|
46
|
+
const LINE_BREAK = /\r\n|[\n\r\v\f\x1c-\x1e\x85\u2028\u2029]/;
|
|
47
|
+
/** Python's str.splitlines(). */
|
|
48
|
+
function splitlines(s) {
|
|
49
|
+
if (s === "")
|
|
50
|
+
return [];
|
|
51
|
+
const parts = s.split(LINE_BREAK);
|
|
52
|
+
if (parts[parts.length - 1] === "")
|
|
53
|
+
parts.pop();
|
|
54
|
+
return parts;
|
|
55
|
+
}
|
|
56
|
+
// Characters JavaScript allows after a backslash in u-mode without a special meaning.
|
|
57
|
+
const SYNTAX = "^$\\.*+?()[]{}|/";
|
|
58
|
+
const WORD_BOUNDARY = `(?:(?<=[${exports.PY_WORD}])(?![${exports.PY_WORD}])|(?<![${exports.PY_WORD}])(?=[${exports.PY_WORD}]))`;
|
|
59
|
+
function translate(src, multiline = false) {
|
|
60
|
+
let out = "";
|
|
61
|
+
let inClass = false;
|
|
62
|
+
let i = 0;
|
|
63
|
+
const unsupported = (what) => {
|
|
64
|
+
throw new SyntaxError(`slopdam: cannot translate ${what} in /${src}/`);
|
|
65
|
+
};
|
|
66
|
+
while (i < src.length) {
|
|
67
|
+
const ch = src[i];
|
|
68
|
+
if (ch === "\\") {
|
|
69
|
+
const n = src[i + 1] ?? unsupported("a trailing backslash");
|
|
70
|
+
i += 2;
|
|
71
|
+
switch (n) {
|
|
72
|
+
case "w":
|
|
73
|
+
out += inClass ? exports.PY_WORD : `[${exports.PY_WORD}]`;
|
|
74
|
+
continue;
|
|
75
|
+
case "s":
|
|
76
|
+
out += inClass ? exports.PY_SPACE : `[${exports.PY_SPACE}]`;
|
|
77
|
+
continue;
|
|
78
|
+
case "d":
|
|
79
|
+
out += "\\p{Nd}";
|
|
80
|
+
continue;
|
|
81
|
+
case "W":
|
|
82
|
+
out += inClass ? unsupported("\\W in a class") : `[^${exports.PY_WORD}]`;
|
|
83
|
+
continue;
|
|
84
|
+
case "S":
|
|
85
|
+
out += inClass ? unsupported("\\S in a class") : `[^${exports.PY_SPACE}]`;
|
|
86
|
+
continue;
|
|
87
|
+
case "D":
|
|
88
|
+
out += inClass ? unsupported("\\D in a class") : "\\P{Nd}";
|
|
89
|
+
continue;
|
|
90
|
+
case "b":
|
|
91
|
+
out += inClass ? "\\x08" : WORD_BOUNDARY;
|
|
92
|
+
continue;
|
|
93
|
+
case "B":
|
|
94
|
+
out += inClass ? unsupported("\\B in a class") : `(?!${WORD_BOUNDARY})`;
|
|
95
|
+
continue;
|
|
96
|
+
case "A":
|
|
97
|
+
out += inClass ? unsupported("\\A in a class") : "(?<![\\s\\S])";
|
|
98
|
+
continue;
|
|
99
|
+
case "Z":
|
|
100
|
+
out += inClass ? unsupported("\\Z in a class") : "(?![\\s\\S])";
|
|
101
|
+
continue;
|
|
102
|
+
case "U":
|
|
103
|
+
out += `\\u{${src.slice(i, i + 8).replace(/^0+(?=.)/, "")}}`;
|
|
104
|
+
i += 8;
|
|
105
|
+
continue;
|
|
106
|
+
case "u":
|
|
107
|
+
out += `\\u${src.slice(i, i + 4)}`;
|
|
108
|
+
i += 4;
|
|
109
|
+
continue;
|
|
110
|
+
case "x":
|
|
111
|
+
out += `\\x${src.slice(i, i + 2)}`;
|
|
112
|
+
i += 2;
|
|
113
|
+
continue;
|
|
114
|
+
case "n":
|
|
115
|
+
case "t":
|
|
116
|
+
case "r":
|
|
117
|
+
case "f":
|
|
118
|
+
case "v":
|
|
119
|
+
case "0":
|
|
120
|
+
out += "\\" + n;
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (/[1-9]/.test(n)) {
|
|
124
|
+
out += "\\" + n;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (/[A-Za-z0-9]/.test(n))
|
|
128
|
+
unsupported(`\\${n}`);
|
|
129
|
+
out += SYNTAX.includes(n) || (inClass && n === "-") ? "\\" + n : n;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
if (inClass) {
|
|
133
|
+
if (ch === "]")
|
|
134
|
+
inClass = false;
|
|
135
|
+
out += ch;
|
|
136
|
+
i++;
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (ch === "[") {
|
|
140
|
+
inClass = true;
|
|
141
|
+
out += ch;
|
|
142
|
+
i++;
|
|
143
|
+
if (src[i] === "^") {
|
|
144
|
+
out += "^";
|
|
145
|
+
i++;
|
|
146
|
+
}
|
|
147
|
+
if (src[i] === "]") {
|
|
148
|
+
out += "\\]";
|
|
149
|
+
i++;
|
|
150
|
+
}
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (ch === ".") {
|
|
154
|
+
out += "[^\\n]";
|
|
155
|
+
i++;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (ch === "^") {
|
|
159
|
+
out += multiline ? "(?<![^\\n])" : "(?<![\\s\\S])";
|
|
160
|
+
i++;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (ch === "$") {
|
|
164
|
+
out += multiline ? "(?![^\\n])" : "(?=\\n?(?![\\s\\S]))";
|
|
165
|
+
i++;
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (src.startsWith("(?P<", i)) {
|
|
169
|
+
out += "(?<";
|
|
170
|
+
i += 4;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (src.startsWith("(?P=", i) || src.startsWith("(?#", i) || /^\(\?[aiLmsux]/.test(src.slice(i))) {
|
|
174
|
+
unsupported("an inline flag or named back reference");
|
|
175
|
+
}
|
|
176
|
+
out += ch;
|
|
177
|
+
i++;
|
|
178
|
+
}
|
|
179
|
+
return out;
|
|
180
|
+
}
|
|
181
|
+
/** A global, Unicode JavaScript RegExp for a spec pattern and its flags. */
|
|
182
|
+
function compile(pattern, flags = []) {
|
|
183
|
+
return new RegExp(translate(pattern, flags.includes("multiline")), "gu");
|
|
184
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Level, Profile } from "./types.js";
|
|
2
|
+
export type DashInline = "comma" | "colon" | "hyphen" | "space";
|
|
3
|
+
/** Python logging levels: a number (WARNING 30, ERROR 40) or a name such as "error". */
|
|
4
|
+
export type LogLevel = number | string;
|
|
5
|
+
export interface SanitizerOptions {
|
|
6
|
+
/** "reader" (default): plain text for people. "dev": grep-friendly text for tools. */
|
|
7
|
+
profile?: Profile;
|
|
8
|
+
/** "basic" (default), "strong" or "strict". */
|
|
9
|
+
level?: Level;
|
|
10
|
+
/** Rule IDs or groups to turn off, such as "emoji" or "dash.run". */
|
|
11
|
+
disable?: Iterable<string>;
|
|
12
|
+
/** What an inline dash becomes in the reader profile. Default "colon". */
|
|
13
|
+
dashInline?: DashInline;
|
|
14
|
+
}
|
|
15
|
+
export interface Finding {
|
|
16
|
+
ruleId: string;
|
|
17
|
+
description: string;
|
|
18
|
+
start: number;
|
|
19
|
+
end: number;
|
|
20
|
+
text: string;
|
|
21
|
+
/** The replacement, or null when the rule only reports. */
|
|
22
|
+
fix: string | null;
|
|
23
|
+
}
|
|
24
|
+
export declare const PROFILES: readonly Profile[];
|
|
25
|
+
export declare const LEVELS: readonly Level[];
|
|
26
|
+
export declare const SPEC_VERSION: number;
|
|
27
|
+
export declare class Sanitizer {
|
|
28
|
+
readonly profile: Profile;
|
|
29
|
+
readonly level: Level;
|
|
30
|
+
private readonly dashInline;
|
|
31
|
+
private readonly rules;
|
|
32
|
+
constructor(options?: SanitizerOptions);
|
|
33
|
+
private context;
|
|
34
|
+
private apply;
|
|
35
|
+
/** Remove AI traces from text. logLevel lets dev drop a status tag the level already says. */
|
|
36
|
+
clean(text: string, logLevel?: LogLevel): string;
|
|
37
|
+
private cleanOnce;
|
|
38
|
+
/** What clean() would change, with spans in the original text (one pass, no tidying). */
|
|
39
|
+
findings(text: string): Finding[];
|
|
40
|
+
}
|
|
41
|
+
/** Clean text with a one-off Sanitizer. */
|
|
42
|
+
export declare function clean(text: string, options?: SanitizerOptions & {
|
|
43
|
+
logLevel?: LogLevel;
|
|
44
|
+
}): string;
|
|
45
|
+
/** All rule IDs in pipeline order. */
|
|
46
|
+
export declare const RULE_IDS: readonly string[];
|