json-6 1.1.3 → 1.1.5
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 +221 -18
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/import.d.mts +2 -0
- package/dist/import.d.mts.map +1 -0
- package/dist/index-esm.d.mts +3 -0
- package/dist/index-esm.d.mts.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3283 -2120
- package/dist/index.min.js +1 -1
- package/dist/index.min.mjs +1 -1
- package/dist/index.mjs +1706 -1615
- package/dist/json6.d.ts +155 -0
- package/dist/json6.d.ts.map +1 -0
- package/dist/register.d.mts +2 -0
- package/dist/register.d.mts.map +1 -0
- package/dist/require.d.ts +3 -0
- package/dist/require.d.ts.map +1 -0
- package/lib/globals.d.ts +7 -0
- package/lib/import.mjs +30 -82
- package/lib/index-esm.mjs +3 -0
- package/lib/json6.js +665 -222
- package/lib/register.mjs +11 -0
- package/lib/require.js +6 -0
- package/package.json +53 -9
package/dist/json6.d.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
declare const JSON6_1: {
|
|
2
|
+
/**
|
|
3
|
+
* @param {string} string
|
|
4
|
+
* @returns {string}
|
|
5
|
+
*/
|
|
6
|
+
escape: (string: string) => string;
|
|
7
|
+
/**
|
|
8
|
+
* @callback Json6Reviver
|
|
9
|
+
* @param {string} key
|
|
10
|
+
* @param {unknown} value
|
|
11
|
+
* @returns {unknown}
|
|
12
|
+
*/
|
|
13
|
+
/**
|
|
14
|
+
* @param {(value: unknown) => void} [cb]
|
|
15
|
+
* @param {Json6Reviver|undefined} [reviver]
|
|
16
|
+
* @param {Json6ParseOptions} [options]
|
|
17
|
+
*/
|
|
18
|
+
begin: (cb?: (value: unknown) => void, reviver?: Json6Reviver | undefined, options?: Json6ParseOptions) => {
|
|
19
|
+
finalError(): void;
|
|
20
|
+
value(): unknown;
|
|
21
|
+
/**
|
|
22
|
+
* @param {Json6ParseOptions} [resetOptions] Options for the next parse.
|
|
23
|
+
* When omitted, the options passed to `begin()` (if any) stay in
|
|
24
|
+
* effect.
|
|
25
|
+
*/
|
|
26
|
+
reset(resetOptions?: Json6ParseOptions): void;
|
|
27
|
+
/**
|
|
28
|
+
* @param {unknown} [msg]
|
|
29
|
+
*/
|
|
30
|
+
write(msg?: unknown): void;
|
|
31
|
+
/**
|
|
32
|
+
* @param {string|undefined|null} [msg]
|
|
33
|
+
* @param {boolean} [complete_at_end]
|
|
34
|
+
*/
|
|
35
|
+
_write(msg?: string | undefined | null, complete_at_end?: boolean): number;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* @param {unknown} msg
|
|
39
|
+
* @param {Json6Reviver} [reviver]
|
|
40
|
+
* @param {Json6ParseOptions} [options]
|
|
41
|
+
*/
|
|
42
|
+
parse: (msg: unknown, reviver?: Json6Reviver, options?: Json6ParseOptions) => unknown;
|
|
43
|
+
/**
|
|
44
|
+
* @typedef {((key: string, value: unknown) => unknown) | Array<string|number> | null} Json6Replacer
|
|
45
|
+
*/
|
|
46
|
+
/**
|
|
47
|
+
* @typedef {object} Json6Stringifier
|
|
48
|
+
* @property {(o?: unknown, r?: Json6Replacer, s?: string|number|null, as?: string) => string|undefined} stringify
|
|
49
|
+
* @property {(q: string) => void} setQuote
|
|
50
|
+
* @property {boolean} ignoreNonEnumerable
|
|
51
|
+
* @property {boolean} sortKeys
|
|
52
|
+
*/
|
|
53
|
+
/** @type {Json6Stringifier|null} */
|
|
54
|
+
stringifierActive: Json6Stringifier | null;
|
|
55
|
+
stringifier: () => {
|
|
56
|
+
/**
|
|
57
|
+
* @param {unknown} [o]
|
|
58
|
+
* @param {Json6Replacer} [r]
|
|
59
|
+
* @param {string|number|null} [s]
|
|
60
|
+
* @param {string} [as]
|
|
61
|
+
* @returns {string|undefined}
|
|
62
|
+
*/
|
|
63
|
+
stringify(o?: unknown, r?: Json6Replacer, s?: string | number | null, as?: string): string | undefined;
|
|
64
|
+
/**
|
|
65
|
+
* @param {string} q
|
|
66
|
+
*/
|
|
67
|
+
setQuote(q: string): void;
|
|
68
|
+
ignoreNonEnumerable: boolean;
|
|
69
|
+
sortKeys: boolean;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* @typedef {object} Json6StringifyOptions
|
|
73
|
+
* @property {boolean} [sortKeys] When `false`, object keys are emitted in their
|
|
74
|
+
* own enumeration order instead of sorted. Defaults to `true`.
|
|
75
|
+
*/
|
|
76
|
+
/**
|
|
77
|
+
* @param {unknown} object
|
|
78
|
+
* @param {Json6Replacer} [replacer]
|
|
79
|
+
* @param {string|number} [space]
|
|
80
|
+
* @param {Json6StringifyOptions} [options]
|
|
81
|
+
* @returns {string|undefined}
|
|
82
|
+
*/
|
|
83
|
+
stringify: (object: unknown, replacer?: Json6Replacer, space?: string | number, options?: Json6StringifyOptions) => string | undefined;
|
|
84
|
+
version: string;
|
|
85
|
+
};
|
|
86
|
+
export = JSON6_1;
|
|
87
|
+
export type Json6Node = {
|
|
88
|
+
n: number;
|
|
89
|
+
buf: null | string;
|
|
90
|
+
};
|
|
91
|
+
export type Json6ParseOptions = {
|
|
92
|
+
/**
|
|
93
|
+
* When `true`, reject the JSON6
|
|
94
|
+
* extensions that are not valid in ECMAScript strict mode, so accepted
|
|
95
|
+
* input can be pasted into a module or `eval`'d unchanged: legacy-octal /
|
|
96
|
+
* leading-zero numbers (`0123`); legacy octal string escapes (`"\012"`,
|
|
97
|
+
* `"\1"`..`"\9"`); literal line terminators inside `'`/`"` strings
|
|
98
|
+
* (backtick strings still allow them); backtick-quoted object keys;
|
|
99
|
+
* unquoted object keys that are neither identifiers nor numbers; and two or
|
|
100
|
+
* more consecutive unary `+`/`-` signs. Defaults to `false`.
|
|
101
|
+
*/
|
|
102
|
+
esStrictCompatible?: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* When `true`, reject an
|
|
105
|
+
* unescaped `${` inside a backtick-quoted string, so a document cannot
|
|
106
|
+
* silently turn into a template-literal substitution if later evaluated as
|
|
107
|
+
* JavaScript. Independent of `esStrictCompatible`. Defaults to `false`.
|
|
108
|
+
*/
|
|
109
|
+
forbidTemplateSubstitution?: boolean;
|
|
110
|
+
/**
|
|
111
|
+
* When `true`, a `//` comment
|
|
112
|
+
* that reaches the end of the document with no terminating line break logs
|
|
113
|
+
* a console warning. The input is accepted either way. Defaults to `false`.
|
|
114
|
+
*/
|
|
115
|
+
warnWithCommentWithoutEOL?: boolean;
|
|
116
|
+
/**
|
|
117
|
+
* When `true`, an unquoted object
|
|
118
|
+
* key that parses as a number is replaced with `String(Number(key))`, so
|
|
119
|
+
* `{1e3: true}` gets the key `"1000"` instead of the source text `"1e3"` --
|
|
120
|
+
* matching what a real JS object literal would produce. Independent of
|
|
121
|
+
* `esStrictCompatible` (does not require it). Note: `Number()` parses a
|
|
122
|
+
* leading-zero digit string like `"0123"` as decimal (123), while a real JS
|
|
123
|
+
* numeric literal `0123` is legacy octal (83) in sloppy mode; combine with
|
|
124
|
+
* `esStrictCompatible` (which forbids leading-zero numbers outright) for a
|
|
125
|
+
* guaranteed match. Defaults to `false`.
|
|
126
|
+
*/
|
|
127
|
+
normalizeNumericKeys?: boolean;
|
|
128
|
+
};
|
|
129
|
+
export type NodeInfo<T> = {
|
|
130
|
+
node: T;
|
|
131
|
+
next: NodeInfo<T> | null | undefined;
|
|
132
|
+
prior: NodeInfo<T> | null;
|
|
133
|
+
};
|
|
134
|
+
export type Context = {
|
|
135
|
+
context: 0 | 1 | 3 | 4;
|
|
136
|
+
elements: Record<string, unknown> | null | undefined;
|
|
137
|
+
element_array: unknown[] | null;
|
|
138
|
+
name: string | null;
|
|
139
|
+
};
|
|
140
|
+
export type Json6Reviver = (key: string, value: unknown) => unknown;
|
|
141
|
+
export type Json6Replacer = ((key: string, value: unknown) => unknown) | Array<string | number> | null;
|
|
142
|
+
export type Json6Stringifier = {
|
|
143
|
+
stringify: (o?: unknown, r?: Json6Replacer, s?: string | number | null, as?: string) => string | undefined;
|
|
144
|
+
setQuote: (q: string) => void;
|
|
145
|
+
ignoreNonEnumerable: boolean;
|
|
146
|
+
sortKeys: boolean;
|
|
147
|
+
};
|
|
148
|
+
export type Json6StringifyOptions = {
|
|
149
|
+
/**
|
|
150
|
+
* When `false`, object keys are emitted in their
|
|
151
|
+
* own enumeration order instead of sorted. Defaults to `true`.
|
|
152
|
+
*/
|
|
153
|
+
sortKeys?: boolean;
|
|
154
|
+
};
|
|
155
|
+
//# sourceMappingURL=json6.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json6.d.ts","sourceRoot":"","sources":["../lib/json6.js"],"names":[],"mappings":";IAuMA;;;OAGG;qBAFQ,MAAM,KACJ,MAAM;IAenB;;;;;OAKG;IAEH;;;;OAIG;iBAHQ,CAAC,KAAK,EAAE,OAAO,KAAK,IAAI,YACxB,YAAY,GAAC,SAAS,YACtB,iBAAiB;QA6I1B,UAAU;QAiBV,KAAK;QAML;;;;WAIG;QACH,KAAK,gBAJM,iBAAiB;QA6C5B;;WAEG;QACH,KAAK,OAFM,OAAO;QA2ClB;;;WAGG;QACH,MAAM,OAHK,MAAM,GAAC,SAAS,GAAC,IAAI,oBACrB,OAAO;;IAk/BpB;;;;OAIG;iBAHQ,OAAO,YACP,YAAY,YACZ,iBAAiB;IAiD5B;;OAEG;IAEH;;;;;;OAMG;IAEH,oCAAoC;uBAAzB,gBAAgB,GAAC,IAAI;;QAY9B;;;;;;WAMG;sBALQ,OAAO,MACP,aAAa,MACb,MAAM,GAAC,MAAM,GAAC,IAAI,OAClB,MAAM,GACJ,MAAM,GAAC,SAAS;QAG7B;;WAEG;oBADQ,MAAM;;;;IAqPnB;;;;OAIG;IAEH;;;;;;OAMG;wBALQ,OAAO,aACP,aAAa,UACb,MAAM,GAAC,MAAM,YACb,qBAAqB,KACnB,MAAM,GAAC,SAAS;;;;AA7tD1B,YAGG,SAAS,GAHF;IAC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AC,EAAE,MAAM,GAAC,MAAM,GAAC,IAAI,EAAE,EAAE,CAAC,EAAE,MAAM,KAAK,MAAM,GAAC,SAAS,CACpG;IAAgC,QAAQ,EAA7B,CAAC,CAAC,EAAE,MAAM,KAAK,IAAI,CAC9B;IAAoB,mBAAmB,EAA5B,OAAO,CAClB;IAAoB,QAAQ,EAAjB,OAAO,CACpB;CAAA,CAAA;AA6QE,YAAkB,qBAAqB,GACvC;;;;;IAAqB,QAAQ,AAA7B,CAEF,EAFa,OAAO,CAEpB;CAAA,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"register.d.mts","sourceRoot":"","sources":["../lib/register.mjs"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"require.d.ts","sourceRoot":"","sources":["../lib/require.js"],"names":[],"mappings":""}
|
package/lib/globals.d.ts
ADDED
package/lib/import.mjs
CHANGED
|
@@ -1,92 +1,40 @@
|
|
|
1
1
|
// import.mjs
|
|
2
|
-
// Node.js
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
// Usage:
|
|
6
|
-
// import {default as config} from "config.json6";
|
|
2
|
+
// Node.js loader for .json6 files
|
|
3
|
+
// - Modern API: resolve/load hooks (Node 20+)
|
|
4
|
+
// - Generates an ES module that parses the JSON6 source and exports default
|
|
7
5
|
|
|
6
|
+
import fs from 'node:fs/promises';
|
|
7
|
+
import { fileURLToPath, URL } from 'node:url';
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import path from "path";
|
|
9
|
+
// Absolute path to this package's CommonJS entry used for parsing
|
|
10
|
+
const LIB_INDEX_ABS = fileURLToPath(new URL('./index.js', import.meta.url));
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
export async function getFormat(url, context, defaultGetFormat) {
|
|
20
|
-
const exten = path.extname( url );
|
|
21
|
-
//if( exten === '' ) return { format:'module' }
|
|
22
|
-
if( exten === ".json6" ){
|
|
23
|
-
return { format: 'module' };
|
|
12
|
+
// Modern loader API
|
|
13
|
+
/** @type {import('node:module').ResolveHook} */
|
|
14
|
+
export async function resolve(specifier, context, nextResolve) {
|
|
15
|
+
if (specifier.endsWith('.json6')) {
|
|
16
|
+
const resolved = new URL(specifier, context.parentURL);
|
|
17
|
+
return { url: resolved.href, shortCircuit: true, format: 'module' };
|
|
24
18
|
}
|
|
25
|
-
return
|
|
19
|
+
return nextResolve(specifier, context);
|
|
26
20
|
}
|
|
27
21
|
|
|
28
|
-
/**
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
22
|
+
/** @type {import('node:module').LoadHook} */
|
|
23
|
+
export async function load(url, context, nextLoad) {
|
|
24
|
+
if (url.endsWith('.json6')) {
|
|
25
|
+
const file = fileURLToPath(url);
|
|
26
|
+
const source = await fs.readFile(file, 'utf8');
|
|
27
|
+
// Build module code that uses createRequire to access our CJS parser
|
|
28
|
+
const code = `
|
|
29
|
+
import { createRequire } from 'node:module';
|
|
30
|
+
import path from 'node:path';
|
|
31
|
+
const require = createRequire(import.meta.url);
|
|
32
|
+
const JSON6 = require(${JSON.stringify(LIB_INDEX_ABS.replace(/\\/g, '/'))});
|
|
33
|
+
const data = JSON6.parse(${JSON.stringify(source)});
|
|
34
|
+
export default data;
|
|
35
|
+
`;
|
|
36
|
+
return { format: 'module', source: code, shortCircuit: true };
|
|
42
37
|
}
|
|
43
|
-
|
|
44
|
-
return defaultGetSource(urlin, context, defaultGetSource);
|
|
38
|
+
return nextLoad(url, context);
|
|
45
39
|
}
|
|
46
40
|
|
|
47
|
-
/**
|
|
48
|
-
* @param {!(string | SharedArrayBuffer | Uint8Array)} source
|
|
49
|
-
* @param {{
|
|
50
|
-
* format: string,
|
|
51
|
-
* url: string,
|
|
52
|
-
* }} context
|
|
53
|
-
* @param {Function} defaultTransformSource
|
|
54
|
-
* @returns {Promise<{ source: !(string | SharedArrayBuffer | Uint8Array) }>}
|
|
55
|
-
*/
|
|
56
|
-
export async function transformSource(source, context, defaultTransformSource) {
|
|
57
|
-
const exten = path.extname( context.url );
|
|
58
|
-
if( exten === ".json6" ){
|
|
59
|
-
return {
|
|
60
|
-
source: "const data = JSON6.parse( '" + escape(source) + "'); export default data;",
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
return defaultTransformSource(source, context, defaultTransformSource);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function escape(string) {
|
|
67
|
-
let output = '';
|
|
68
|
-
if( !string ) return string;
|
|
69
|
-
for( let n = 0; n < string.length; n++ ) {
|
|
70
|
-
const ch = string[n];
|
|
71
|
-
if( ch === '\n' ){
|
|
72
|
-
output += "\\n";
|
|
73
|
-
} else {
|
|
74
|
-
if( ( ch === '"' ) || ( ch === '\\' ) || ( ch === '`' )|| ( ch === '\'' )) {
|
|
75
|
-
output += '\\';
|
|
76
|
-
}
|
|
77
|
-
output += ch;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return output;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* @returns {string} Code to run before application startup
|
|
85
|
-
*/
|
|
86
|
-
// Preloads JSON6 as a global resource; which is then used in the transformed source above.
|
|
87
|
-
export function getGlobalPreloadCode() {
|
|
88
|
-
return `\
|
|
89
|
-
const { createRequire } = getBuiltin('module');
|
|
90
|
-
const requireJSON6 = createRequire('${escape(url.fileURLToPath( import.meta.url ))}');
|
|
91
|
-
globalThis.JSON6 = requireJSON6( "../lib/index.js" );`;
|
|
92
|
-
}
|