@plurnk/plurnk-mimetypes-text-csv 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.md +24 -0
- package/dist/TextCsv.d.ts +8 -0
- package/dist/TextCsv.d.ts.map +1 -0
- package/dist/TextCsv.js +109 -0
- package/dist/TextCsv.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 PossumTech Laboratories, LLC
|
|
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.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @plurnk/plurnk-mimetypes-text-csv
|
|
2
|
+
|
|
3
|
+
`text/csv` mimetype handler for the [plurnk](https://github.com/plurnk) ecosystem.
|
|
4
|
+
|
|
5
|
+
## install
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm i @plurnk/plurnk-mimetypes-text-csv
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## what it does
|
|
12
|
+
|
|
13
|
+
- `validate(content)` walks all records with the bundled RFC 4180 tokenizer; throws on unbalanced quotes or non-uniform column count.
|
|
14
|
+
- `extract(content)` emits one `field` symbol per header column (the first record), at line 1.
|
|
15
|
+
|
|
16
|
+
CSV's structural signal is the header row's column names — that's what surfaces in `symbols`. The actual data body is best previewed via the framework's raw-content fallback.
|
|
17
|
+
|
|
18
|
+
## why no parser dependency
|
|
19
|
+
|
|
20
|
+
CSV is the textbook case where RFC 4180 fits in <100 LOC of careful hand-rolled tokenizer and the available libraries all carry transitive dependencies that don't earn their keep for header extraction. The tokenizer (`parseAll`) is exported for re-use.
|
|
21
|
+
|
|
22
|
+
## license
|
|
23
|
+
|
|
24
|
+
MIT.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { BaseHandler } from "@plurnk/plurnk-mimetypes";
|
|
2
|
+
import type { MimeSymbol } from "@plurnk/plurnk-mimetypes";
|
|
3
|
+
export default class TextCsv extends BaseHandler {
|
|
4
|
+
validate(content: string): void;
|
|
5
|
+
extract(content: string): MimeSymbol[];
|
|
6
|
+
}
|
|
7
|
+
export declare function parseAll(content: string): string[][];
|
|
8
|
+
//# sourceMappingURL=TextCsv.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TextCsv.d.ts","sourceRoot":"","sources":["../src/TextCsv.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AACvD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAC;AAY3D,MAAM,CAAC,OAAO,OAAO,OAAQ,SAAQ,WAAW;IAC5C,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAa/B,OAAO,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,EAAE;CAUzC;AAMD,wBAAgB,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,CAyEpD"}
|
package/dist/TextCsv.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { BaseHandler } from "@plurnk/plurnk-mimetypes";
|
|
2
|
+
// text/csv handler. CSV's structural signal is the header row's column names;
|
|
3
|
+
// no library dependency — RFC 4180 is small enough to hand-roll correctly.
|
|
4
|
+
//
|
|
5
|
+
// validate(): walk all records, throw on unbalanced quotes or non-uniform
|
|
6
|
+
// column count across rows.
|
|
7
|
+
// extract(): emit one `field` symbol per header column, at line 1.
|
|
8
|
+
//
|
|
9
|
+
// Quote-stripped header tokens become field names. Empty headers become
|
|
10
|
+
// empty-named symbols (still emitted — surfacing the column count is
|
|
11
|
+
// meaningful even when headers are blank).
|
|
12
|
+
export default class TextCsv extends BaseHandler {
|
|
13
|
+
validate(content) {
|
|
14
|
+
const records = parseAll(content);
|
|
15
|
+
if (records.length === 0)
|
|
16
|
+
return;
|
|
17
|
+
const expected = records[0].length;
|
|
18
|
+
for (let i = 1; i < records.length; i += 1) {
|
|
19
|
+
if (records[i].length !== expected) {
|
|
20
|
+
throw new SyntaxError(`CSV row ${i + 1} has ${records[i].length} columns; header has ${expected}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
extract(content) {
|
|
25
|
+
const records = parseAll(content);
|
|
26
|
+
if (records.length === 0)
|
|
27
|
+
return [];
|
|
28
|
+
return records[0].map((name) => ({
|
|
29
|
+
name,
|
|
30
|
+
kind: "field",
|
|
31
|
+
line: 1,
|
|
32
|
+
endLine: 1,
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
// RFC 4180 tokenizer. Walks character by character, tracking quoted state,
|
|
37
|
+
// handling escaped double-quotes inside quoted fields, accepting CR/LF/CRLF
|
|
38
|
+
// line endings between unquoted records, and treating commas inside quoted
|
|
39
|
+
// fields as literal characters. Throws on unbalanced quotes.
|
|
40
|
+
export function parseAll(content) {
|
|
41
|
+
const records = [];
|
|
42
|
+
let row = [];
|
|
43
|
+
let field = "";
|
|
44
|
+
let inQuotes = false;
|
|
45
|
+
let i = 0;
|
|
46
|
+
while (i < content.length) {
|
|
47
|
+
const ch = content[i];
|
|
48
|
+
if (inQuotes) {
|
|
49
|
+
if (ch === '"') {
|
|
50
|
+
if (i + 1 < content.length && content[i + 1] === '"') {
|
|
51
|
+
// Escaped double-quote inside a quoted field.
|
|
52
|
+
field += '"';
|
|
53
|
+
i += 2;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
// End of quoted field.
|
|
57
|
+
inQuotes = false;
|
|
58
|
+
i += 1;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
field += ch;
|
|
62
|
+
i += 1;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (ch === '"') {
|
|
66
|
+
inQuotes = true;
|
|
67
|
+
i += 1;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (ch === ",") {
|
|
71
|
+
row.push(field);
|
|
72
|
+
field = "";
|
|
73
|
+
i += 1;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (ch === "\r") {
|
|
77
|
+
// Treat CR (alone) or CRLF as record terminator.
|
|
78
|
+
row.push(field);
|
|
79
|
+
field = "";
|
|
80
|
+
records.push(row);
|
|
81
|
+
row = [];
|
|
82
|
+
i += 1;
|
|
83
|
+
if (i < content.length && content[i] === "\n")
|
|
84
|
+
i += 1;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (ch === "\n") {
|
|
88
|
+
row.push(field);
|
|
89
|
+
field = "";
|
|
90
|
+
records.push(row);
|
|
91
|
+
row = [];
|
|
92
|
+
i += 1;
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
field += ch;
|
|
96
|
+
i += 1;
|
|
97
|
+
}
|
|
98
|
+
if (inQuotes) {
|
|
99
|
+
throw new SyntaxError("Unbalanced quote in CSV content");
|
|
100
|
+
}
|
|
101
|
+
// Flush the final partial row if there's content (handles files without
|
|
102
|
+
// a trailing newline).
|
|
103
|
+
if (field !== "" || row.length > 0) {
|
|
104
|
+
row.push(field);
|
|
105
|
+
records.push(row);
|
|
106
|
+
}
|
|
107
|
+
return records;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=TextCsv.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TextCsv.js","sourceRoot":"","sources":["../src/TextCsv.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAGvD,8EAA8E;AAC9E,2EAA2E;AAC3E,EAAE;AACF,0EAA0E;AAC1E,wCAAwC;AACxC,mEAAmE;AACnE,EAAE;AACF,wEAAwE;AACxE,qEAAqE;AACrE,2CAA2C;AAC3C,MAAM,CAAC,OAAO,OAAO,OAAQ,SAAQ,WAAW;IAC5C,QAAQ,CAAC,OAAe;QACpB,MAAM,OAAO,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;QAClC,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO;QACjC,MAAM,QAAQ,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;QACnC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;YACzC,IAAI,OAAO,CAAC,CAAC,CAAC,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;gBACjC,MAAM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wE;IACxE,uBAAuB;IACvB,IAAI,KAAK,KAAK,EAAE,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACjC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAChB,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACtB,CAAC;IAED,OAAO,OAAO,CAAC;AACnB,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@plurnk/plurnk-mimetypes-text-csv",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "text/csv mimetype handler for plurnk-service. Homebrew RFC 4180 tokenizer; no parser dependency.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=25"
|
|
12
|
+
},
|
|
13
|
+
"plurnk": {
|
|
14
|
+
"kind": "mimetype",
|
|
15
|
+
"handlers": [
|
|
16
|
+
{ "name": "text/csv", "glyph": "📊", "extensions": [".csv"] }
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist/**/*",
|
|
28
|
+
"README.md"
|
|
29
|
+
],
|
|
30
|
+
"scripts": {
|
|
31
|
+
"test:lint": "tsc --noEmit",
|
|
32
|
+
"test:unit": "node --test src/**/*.test.ts",
|
|
33
|
+
"test": "npm run test:lint && npm run test:unit",
|
|
34
|
+
"build:dist": "tsc -p tsconfig.build.json",
|
|
35
|
+
"build": "npm run build:dist",
|
|
36
|
+
"prepare": "npm run build"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@plurnk/plurnk-mimetypes": "^0.2.1"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/node": "^25.8.0",
|
|
43
|
+
"typescript": "^6.0.3"
|
|
44
|
+
}
|
|
45
|
+
}
|