@plurnk/plurnk-mimetypes-application-json 0.2.0 → 0.4.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.
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { BaseHandler } from "@plurnk/plurnk-mimetypes";
|
|
2
|
-
import type { MimeSymbol } from "@plurnk/plurnk-mimetypes";
|
|
2
|
+
import type { HandlerContent, MimeSymbol, QueryDialect, QueryMatch } from "@plurnk/plurnk-mimetypes";
|
|
3
3
|
export default class ApplicationJson extends BaseHandler {
|
|
4
4
|
validate(content: string): void;
|
|
5
|
-
|
|
5
|
+
extractRaw(content: string): MimeSymbol[];
|
|
6
|
+
query(content: HandlerContent, dialect: QueryDialect, pattern: string, flags?: string): Promise<QueryMatch[]>;
|
|
6
7
|
}
|
|
7
8
|
//# sourceMappingURL=ApplicationJson.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApplicationJson.d.ts","sourceRoot":"","sources":["../src/ApplicationJson.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"ApplicationJson.d.ts","sourceRoot":"","sources":["../src/ApplicationJson.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,WAAW,EAGd,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EACR,cAAc,EACd,UAAU,EACV,YAAY,EACZ,UAAU,EACb,MAAM,0BAA0B,CAAC;AAsBlC,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,WAAW;IAC3C,QAAQ,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAe/B,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,UAAU,EAAE;IAsBnC,KAAK,CAChB,OAAO,EAAE,cAAc,EACvB,OAAO,EAAE,YAAY,EACrB,OAAO,EAAE,MAAM,EACf,KAAK,CAAC,EAAE,MAAM,GACf,OAAO,CAAC,UAAU,EAAE,CAAC;CAiC3B"}
|
package/dist/ApplicationJson.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { BaseHandler } from "@plurnk/plurnk-mimetypes";
|
|
2
|
-
import { parseTree, printParseErrorCode } from "jsonc-parser";
|
|
1
|
+
import { BaseHandler, queryJsonpathObject, QueryParseFailureError, } from "@plurnk/plurnk-mimetypes";
|
|
2
|
+
import { findNodeAtLocation, getNodeValue, parseTree, printParseErrorCode, } from "jsonc-parser";
|
|
3
3
|
// One class serves two mimetype names — application/json (strict) and
|
|
4
4
|
// application/jsonc (comments + trailing commas allowed). The framework
|
|
5
5
|
// constructs one instance per registered name; this.mimetype distinguishes
|
|
@@ -8,10 +8,10 @@ import { parseTree, printParseErrorCode } from "jsonc-parser";
|
|
|
8
8
|
// validate(): strict for application/json (no comments, no trailing commas);
|
|
9
9
|
// permissive for application/jsonc. Errors propagate per SPEC §7.
|
|
10
10
|
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
11
|
+
// extractRaw(): every key occurrence at every depth as a `field` symbol, with
|
|
12
|
+
// line numbers from jsonc-parser's positional tree (Node.offset).
|
|
13
|
+
// No regex tokenization, no escape-handling reinvention — the
|
|
14
|
+
// parser does it.
|
|
15
15
|
export default class ApplicationJson extends BaseHandler {
|
|
16
16
|
validate(content) {
|
|
17
17
|
const errors = [];
|
|
@@ -26,7 +26,7 @@ export default class ApplicationJson extends BaseHandler {
|
|
|
26
26
|
const { line, column } = offsetToLineCol(content, first.offset);
|
|
27
27
|
throw new SyntaxError(`${printParseErrorCode(first.error)} at line ${line}:${column}`);
|
|
28
28
|
}
|
|
29
|
-
|
|
29
|
+
extractRaw(content) {
|
|
30
30
|
const errors = [];
|
|
31
31
|
const allowsRelaxation = this.mimetype === "application/jsonc";
|
|
32
32
|
const tree = parseTree(content, errors, {
|
|
@@ -39,6 +39,63 @@ export default class ApplicationJson extends BaseHandler {
|
|
|
39
39
|
collectKeys(tree, content, symbols);
|
|
40
40
|
return symbols;
|
|
41
41
|
}
|
|
42
|
+
// Override jsonpath dispatch so queries hit the parsed JSON value (the
|
|
43
|
+
// actual data the model is asking about) rather than the bare-leaves
|
|
44
|
+
// outline of keys. Line numbers for matches come from jsonc-parser's
|
|
45
|
+
// positional tree: findNodeAtLocation walks segments to the result node,
|
|
46
|
+
// and the node's offset maps to a source line.
|
|
47
|
+
//
|
|
48
|
+
// regex/glob inherit BaseHandler's defaults (against the raw JSON text).
|
|
49
|
+
// xpath inherits the unsupported-dialect throw.
|
|
50
|
+
async query(content, dialect, pattern, flags) {
|
|
51
|
+
if (dialect === "jsonpath") {
|
|
52
|
+
if (typeof content !== "string") {
|
|
53
|
+
throw new QueryParseFailureError({
|
|
54
|
+
mimetype: this.mimetype,
|
|
55
|
+
cause: new TypeError("application/json content must be a string"),
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
const errors = [];
|
|
59
|
+
const allowsRelaxation = this.mimetype === "application/jsonc";
|
|
60
|
+
const tree = parseTree(content, errors, {
|
|
61
|
+
allowTrailingComma: allowsRelaxation,
|
|
62
|
+
disallowComments: !allowsRelaxation,
|
|
63
|
+
});
|
|
64
|
+
if (tree === undefined || errors.length > 0) {
|
|
65
|
+
throw new QueryParseFailureError({
|
|
66
|
+
mimetype: this.mimetype,
|
|
67
|
+
cause: errors[0]
|
|
68
|
+
? new SyntaxError(printParseErrorCode(errors[0].error))
|
|
69
|
+
: new SyntaxError("empty JSON"),
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const value = getNodeValue(tree);
|
|
73
|
+
const lineFor = (path) => {
|
|
74
|
+
const segments = pathToSegments(path);
|
|
75
|
+
const node = findNodeAtLocation(tree, segments);
|
|
76
|
+
if (node === undefined)
|
|
77
|
+
return 1;
|
|
78
|
+
return offsetToLineCol(content, node.offset).line;
|
|
79
|
+
};
|
|
80
|
+
return queryJsonpathObject(value, pattern, lineFor);
|
|
81
|
+
}
|
|
82
|
+
return super.query(content, dialect, pattern, flags);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// Convert a jsonpath-plus "path" string ($['users'][0]['name']) into the
|
|
86
|
+
// segment array shape that jsonc-parser's findNodeAtLocation accepts
|
|
87
|
+
// (['users', 0, 'name']).
|
|
88
|
+
function pathToSegments(path) {
|
|
89
|
+
const segments = [];
|
|
90
|
+
const re = /\['([^']*)'\]|\[(\d+)\]/g;
|
|
91
|
+
let m;
|
|
92
|
+
while ((m = re.exec(path)) !== null) {
|
|
93
|
+
if (m[1] !== undefined)
|
|
94
|
+
segments.push(m[1]);
|
|
95
|
+
else
|
|
96
|
+
segments.push(Number(m[2]));
|
|
97
|
+
}
|
|
98
|
+
return segments;
|
|
42
99
|
}
|
|
43
100
|
// Walk a jsonc-parser Node tree and emit a field symbol for every property
|
|
44
101
|
// key encountered at every depth. Each property node has a `children` pair:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApplicationJson.js","sourceRoot":"","sources":["../src/ApplicationJson.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"ApplicationJson.js","sourceRoot":"","sources":["../src/ApplicationJson.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,WAAW,EACX,mBAAmB,EACnB,sBAAsB,GACzB,MAAM,0BAA0B,CAAC;AAOlC,OAAO,EACH,kBAAkB,EAClB,YAAY,EAGZ,SAAS,EACT,mBAAmB,GACtB,MAAM,cAAc,CAAC;AAEtB,sEAAsE;AACtE,wEAAwE;AACxE,2EAA2E;AAC3E,mBAAmB;AACnB,EAAE;AACF,6EAA6E;AAC7E,8EAA8E;AAC9E,EAAE;AACF,8EAA8E;AAC9E,gFAAgF;AAChF,4EAA4E;AAC5E,gCAAgC;AAChC,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,WAAW;IAC3C,QAAQ,CAAC,OAAe;QAC7B,MAAM,MAAM,GAAiB,EAAE,CAAC;QAChC,MAAM,gBAAgB,GAAG,IAAI,CAAC,QAAQ,KAAK,mBAAmB,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AChB,CAAC,CAAC;QACP,CAAC;QACD,8CAA8C;QAC9C,MAAM,SAAS,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;QACnC,IAAI,SAAS;YAAE,WAAW,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;QACrD,OAAO;IACX,CAAC;IAED,8DAA8D;IAC9D,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;QAChB,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAChC,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;QACtC,CAAC;IACL,CAAC;AACL,CAAC;AAED,oDAAoD;AACpD,SAAS,eAAe,CAAC,OAAe,EAAE,MAAc;IACpD,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;IAC/C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;QAChC,IAAI,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,IAAI,EAAE,CAAC;YACjC,IAAI,IAAI,CAAC,CAAC;YACV,MAAM,GAAG,CAAC,CAAC;QACf,CAAC;aAAM,CAAC;YACJ,MAAM,IAAI,CAAC,CAAC;QAChB,CAAC;IACL,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC;AAC5B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plurnk/plurnk-mimetypes-application-json",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "application/json and application/jsonc mimetype handler for plurnk-service.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -13,8 +13,20 @@
|
|
|
13
13
|
"plurnk": {
|
|
14
14
|
"kind": "mimetype",
|
|
15
15
|
"handlers": [
|
|
16
|
-
{
|
|
17
|
-
|
|
16
|
+
{
|
|
17
|
+
"name": "application/json",
|
|
18
|
+
"glyph": "📋",
|
|
19
|
+
"extensions": [
|
|
20
|
+
".json"
|
|
21
|
+
]
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"name": "application/jsonc",
|
|
25
|
+
"glyph": "📋",
|
|
26
|
+
"extensions": [
|
|
27
|
+
".jsonc"
|
|
28
|
+
]
|
|
29
|
+
}
|
|
18
30
|
]
|
|
19
31
|
},
|
|
20
32
|
"exports": {
|
|
@@ -37,7 +49,7 @@
|
|
|
37
49
|
"prepare": "npm run build"
|
|
38
50
|
},
|
|
39
51
|
"dependencies": {
|
|
40
|
-
"@plurnk/plurnk-mimetypes": "^0.
|
|
52
|
+
"@plurnk/plurnk-mimetypes": "^0.6.0",
|
|
41
53
|
"jsonc-parser": "^3.3.1"
|
|
42
54
|
},
|
|
43
55
|
"devDependencies": {
|