@uruhalushia/rule-converter-napi 0.1.1 → 0.1.3
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 +34 -2
- package/index.d.ts +39 -10
- package/index.js +21 -21
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -13,7 +13,12 @@ pnpm --dir napi build
|
|
|
13
13
|
|
|
14
14
|
```js
|
|
15
15
|
import { writeFileSync } from 'node:fs'
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
convertPayloadStringToMrs,
|
|
18
|
+
convertPayloadStringToString,
|
|
19
|
+
convertFileToBuffer,
|
|
20
|
+
convertFileToPath,
|
|
21
|
+
} from '@uruhalushia/rule-converter-napi'
|
|
17
22
|
|
|
18
23
|
const payload = `
|
|
19
24
|
payload:
|
|
@@ -35,9 +40,28 @@ for (const output of result.outputs) {
|
|
|
35
40
|
writeFileSync(`${output.behavior}.mrs`, output.bytes)
|
|
36
41
|
}
|
|
37
42
|
|
|
43
|
+
const text = convertPayloadStringToString(payload, {
|
|
44
|
+
inputTarget: 'mihomo',
|
|
45
|
+
inputFormat: 'yaml',
|
|
46
|
+
inputBehavior: 'classical',
|
|
47
|
+
outputTarget: 'general',
|
|
48
|
+
outputFormat: 'ruleset',
|
|
49
|
+
outputBehavior: 'classical',
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
console.log(text.outputs[0].text)
|
|
53
|
+
|
|
54
|
+
const srs = convertFileToBuffer('rules.yaml', {
|
|
55
|
+
outputTarget: 'sing-box',
|
|
56
|
+
outputFormat: 'srs',
|
|
57
|
+
outputBehavior: 'classical',
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
writeFileSync('rules.srs', srs.outputs[0].buffer)
|
|
61
|
+
|
|
38
62
|
const written = convertFileToPath('rules.yaml', 'dist/rules.list', {
|
|
39
63
|
outputTarget: 'general',
|
|
40
|
-
outputFormat: '
|
|
64
|
+
outputFormat: 'ruleset',
|
|
41
65
|
outputBehavior: 'classical',
|
|
42
66
|
})
|
|
43
67
|
|
|
@@ -59,6 +83,12 @@ convertFileToPath(['/path/rules-a.yaml', '/path/rules-b.yaml'], 'dist/ad.mrs', {
|
|
|
59
83
|
- `convertPayloadToMrs(payload, options?)`: accepts `Uint8Array` and returns generated MRS files in memory.
|
|
60
84
|
- `convertPayloadStringToMrs(payload, options?)`: accepts a string and returns generated MRS files in memory.
|
|
61
85
|
- `convertFileToMrs(input, options?)`: reads one file, directory, wildcard, or path array and returns generated MRS files in memory.
|
|
86
|
+
- `convertPayloadToBuffer(payload, options?)`: accepts `Uint8Array` and returns generated files as Node.js `Buffer` objects in memory.
|
|
87
|
+
- `convertPayloadStringToBuffer(payload, options?)`: accepts a string and returns generated files as Node.js `Buffer` objects in memory.
|
|
88
|
+
- `convertFileToBuffer(input, options?)`: reads one file, directory, wildcard, or path array and returns generated files as Node.js `Buffer` objects in memory.
|
|
89
|
+
- `convertPayloadToString(payload, options?)`: accepts `Uint8Array` and returns generated text output as strings.
|
|
90
|
+
- `convertPayloadStringToString(payload, options?)`: accepts a string and returns generated text output as strings.
|
|
91
|
+
- `convertFileToString(input, options?)`: reads one file, directory, wildcard, or path array and returns generated text output as strings.
|
|
62
92
|
- `convertFileToPath(input, output, options?)`: writes converted outputs to disk.
|
|
63
93
|
|
|
64
94
|
```ts
|
|
@@ -83,6 +113,8 @@ Defaults:
|
|
|
83
113
|
|
|
84
114
|
mihomo MRS output supports only `domain` and `ip`. sing-box JSON/SRS is available with `outputTarget: 'sing-box'` and `outputFormat: 'json' | 'srs'`.
|
|
85
115
|
|
|
116
|
+
String output supports text formats only: `text`, `yaml`, `json`, `domainset`, `ruleset`, and `ipset`. Use the buffer functions for binary `mrs` and `srs` output.
|
|
117
|
+
|
|
86
118
|
`mihomo + text/yaml + domain` uses mihomo/Clash domain wildcard syntax such as `+.example.com`; `general + domainset + domain` uses domain-set syntax where `.example.com` means the domain itself and all subdomains.
|
|
87
119
|
|
|
88
120
|
`no-resolve` is preserved only between mixed text, mihomo YAML, and Egern ruleset YAML. MRS, sing-box JSON/SRS, and domain-set output do not have a field for it.
|
package/index.d.ts
CHANGED
|
@@ -1,21 +1,31 @@
|
|
|
1
1
|
/* auto-generated by NAPI-RS */
|
|
2
2
|
/* eslint-disable */
|
|
3
|
+
export interface ConvertBufferOutput {
|
|
4
|
+
behavior: 'domain' | 'ip'
|
|
5
|
+
count: number
|
|
6
|
+
buffer: Uint8Array
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ConvertBufferResult {
|
|
10
|
+
outputs: Array<ConvertBufferOutput>
|
|
11
|
+
skipped: Array<SkippedRule>
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export declare function convertFileToBuffer(input: string | string[], options?: ConvertOptions | undefined | null): ConvertBufferResult
|
|
15
|
+
|
|
3
16
|
export declare function convertFileToMrs(input: string | string[], options?: ConvertOptions | undefined | null): ConvertResult
|
|
4
17
|
|
|
5
18
|
export declare function convertFileToPath(input: string | string[], output: string, options?: ConvertOptions | undefined | null): WriteResult
|
|
6
19
|
|
|
7
|
-
export
|
|
8
|
-
export type RuleFormat = 'mrs' | 'text' | 'yaml' | 'json' | 'srs' | 'domainset' | 'ruleset' | 'ipset'
|
|
9
|
-
export type InputBehavior = 'auto' | 'domain' | 'ip' | 'classical'
|
|
10
|
-
export type OutputBehavior = 'auto' | 'domain' | 'ip' | 'classical'
|
|
20
|
+
export declare function convertFileToString(input: string | string[], options?: ConvertOptions | undefined | null): ConvertStringResult
|
|
11
21
|
|
|
12
22
|
export interface ConvertOptions {
|
|
13
|
-
inputTarget?:
|
|
14
|
-
inputFormat?:
|
|
15
|
-
inputBehavior?:
|
|
16
|
-
outputTarget?:
|
|
17
|
-
outputFormat?:
|
|
18
|
-
outputBehavior?:
|
|
23
|
+
inputTarget?: 'mihomo' | 'general' | 'egern' | 'sing-box'
|
|
24
|
+
inputFormat?: 'yaml' | 'mrs' | 'text' | 'json' | 'srs'
|
|
25
|
+
inputBehavior?: 'auto' | 'domain' | 'ip' | 'classical'
|
|
26
|
+
outputTarget?: 'mihomo' | 'general' | 'egern' | 'sing-box'
|
|
27
|
+
outputFormat?: 'mrs' | 'text' | 'yaml' | 'json' | 'srs' | 'domainset' | 'ruleset' | 'ipset'
|
|
28
|
+
outputBehavior?: 'auto' | 'domain' | 'ip' | 'classical'
|
|
19
29
|
}
|
|
20
30
|
|
|
21
31
|
export interface ConvertOutput {
|
|
@@ -24,15 +34,34 @@ export interface ConvertOutput {
|
|
|
24
34
|
bytes: Uint8Array
|
|
25
35
|
}
|
|
26
36
|
|
|
37
|
+
export declare function convertPayloadStringToBuffer(payload: string, options?: ConvertOptions | undefined | null): ConvertBufferResult
|
|
38
|
+
|
|
27
39
|
export declare function convertPayloadStringToMrs(payload: string, options?: ConvertOptions | undefined | null): ConvertResult
|
|
28
40
|
|
|
41
|
+
export declare function convertPayloadStringToString(payload: string, options?: ConvertOptions | undefined | null): ConvertStringResult
|
|
42
|
+
|
|
43
|
+
export declare function convertPayloadToBuffer(payload: Uint8Array, options?: ConvertOptions | undefined | null): ConvertBufferResult
|
|
44
|
+
|
|
29
45
|
export declare function convertPayloadToMrs(payload: Uint8Array, options?: ConvertOptions | undefined | null): ConvertResult
|
|
30
46
|
|
|
47
|
+
export declare function convertPayloadToString(payload: Uint8Array, options?: ConvertOptions | undefined | null): ConvertStringResult
|
|
48
|
+
|
|
31
49
|
export interface ConvertResult {
|
|
32
50
|
outputs: Array<ConvertOutput>
|
|
33
51
|
skipped: Array<SkippedRule>
|
|
34
52
|
}
|
|
35
53
|
|
|
54
|
+
export interface ConvertStringOutput {
|
|
55
|
+
behavior: 'domain' | 'ip'
|
|
56
|
+
count: number
|
|
57
|
+
text: string
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface ConvertStringResult {
|
|
61
|
+
outputs: Array<ConvertStringOutput>
|
|
62
|
+
skipped: Array<SkippedRule>
|
|
63
|
+
}
|
|
64
|
+
|
|
36
65
|
export interface SkippedRule {
|
|
37
66
|
rule: string
|
|
38
67
|
reason: string
|
package/index.js
CHANGED
|
@@ -5,28 +5,21 @@
|
|
|
5
5
|
import { existsSync } from 'node:fs'
|
|
6
6
|
import { join } from 'node:path'
|
|
7
7
|
import { createRequire } from 'node:module'
|
|
8
|
-
import packageJson from './package.json' with { type: 'json' }
|
|
9
8
|
|
|
10
9
|
const require = createRequire(import.meta.url)
|
|
11
10
|
const __dirname = new URL('.', import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1')
|
|
12
11
|
|
|
13
12
|
const packageName = '@uruhalushia/rule-converter-napi'
|
|
14
|
-
const packageVersion = packageJson.version
|
|
15
13
|
const binaryName = 'rule-converter'
|
|
16
14
|
const loadErrors = []
|
|
17
15
|
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
-
return require('node:child_process').execSync('ldd --version', { encoding: 'utf8' }).includes('musl')
|
|
21
|
-
} catch (_) {
|
|
22
|
-
return false
|
|
23
|
-
}
|
|
16
|
+
function shouldCheckVersion() {
|
|
17
|
+
return process.env.NAPI_RS_ENFORCE_VERSION_CHECK && process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0'
|
|
24
18
|
}
|
|
25
19
|
|
|
26
20
|
function requireLocal(tuple) {
|
|
27
21
|
const filename = join(__dirname, `${binaryName}.${tuple}.node`)
|
|
28
22
|
if (!existsSync(filename)) {
|
|
29
|
-
loadErrors.push(new Error(`Native binding not found: ${filename}`))
|
|
30
23
|
return null
|
|
31
24
|
}
|
|
32
25
|
try {
|
|
@@ -41,14 +34,9 @@ function requirePackage(tuple) {
|
|
|
41
34
|
const nativePackage = `${packageName}-${tuple}`
|
|
42
35
|
try {
|
|
43
36
|
const binding = require(nativePackage)
|
|
44
|
-
|
|
45
|
-
if (
|
|
46
|
-
bindingPackageVersion !== packageVersion &&
|
|
47
|
-
process.env.NAPI_RS_ENFORCE_VERSION_CHECK &&
|
|
48
|
-
process.env.NAPI_RS_ENFORCE_VERSION_CHECK !== '0'
|
|
49
|
-
) {
|
|
37
|
+
if (shouldCheckVersion() && require(`${nativePackage}/package.json`).version !== require('./package.json').version) {
|
|
50
38
|
throw new Error(
|
|
51
|
-
`Native binding package version mismatch
|
|
39
|
+
`Native binding package version mismatch. You can reinstall dependencies to fix this issue.`,
|
|
52
40
|
)
|
|
53
41
|
}
|
|
54
42
|
return binding
|
|
@@ -62,6 +50,13 @@ function requireBinding(tuple) {
|
|
|
62
50
|
return requireLocal(tuple) || requirePackage(tuple)
|
|
63
51
|
}
|
|
64
52
|
|
|
53
|
+
function requireLinuxBinding(gnuTuple, muslTuple) {
|
|
54
|
+
if (existsSync('/etc/alpine-release')) {
|
|
55
|
+
return requireBinding(muslTuple) || requireBinding(gnuTuple)
|
|
56
|
+
}
|
|
57
|
+
return requireBinding(gnuTuple) || requireBinding(muslTuple)
|
|
58
|
+
}
|
|
59
|
+
|
|
65
60
|
function requireNative() {
|
|
66
61
|
if (process.env.NAPI_RS_NATIVE_LIBRARY_PATH) {
|
|
67
62
|
try {
|
|
@@ -78,11 +73,10 @@ function requireNative() {
|
|
|
78
73
|
if (process.arch === 'x64') return requireBinding('darwin-x64')
|
|
79
74
|
if (process.arch === 'arm64') return requireBinding('darwin-arm64')
|
|
80
75
|
} else if (process.platform === 'linux') {
|
|
81
|
-
|
|
82
|
-
if (process.arch === '
|
|
83
|
-
if (process.arch === '
|
|
84
|
-
if (process.arch === '
|
|
85
|
-
if (process.arch === 'loong64') return requireBinding(musl ? 'linux-loong64-musl' : 'linux-loong64-gnu')
|
|
76
|
+
if (process.arch === 'x64') return requireLinuxBinding('linux-x64-gnu', 'linux-x64-musl')
|
|
77
|
+
if (process.arch === 'arm64') return requireLinuxBinding('linux-arm64-gnu', 'linux-arm64-musl')
|
|
78
|
+
if (process.arch === 'riscv64') return requireLinuxBinding('linux-riscv64-gnu', 'linux-riscv64-musl')
|
|
79
|
+
if (process.arch === 'loong64') return requireLinuxBinding('linux-loong64-gnu', 'linux-loong64-musl')
|
|
86
80
|
}
|
|
87
81
|
|
|
88
82
|
loadErrors.push(new Error(`Unsupported OS or architecture: ${process.platform} ${process.arch}`))
|
|
@@ -100,5 +94,11 @@ if (!nativeBinding) {
|
|
|
100
94
|
export default nativeBinding
|
|
101
95
|
export const convertPayloadToMrs = nativeBinding.convertPayloadToMrs
|
|
102
96
|
export const convertPayloadStringToMrs = nativeBinding.convertPayloadStringToMrs
|
|
97
|
+
export const convertPayloadToBuffer = nativeBinding.convertPayloadToBuffer
|
|
98
|
+
export const convertPayloadStringToBuffer = nativeBinding.convertPayloadStringToBuffer
|
|
99
|
+
export const convertPayloadToString = nativeBinding.convertPayloadToString
|
|
100
|
+
export const convertPayloadStringToString = nativeBinding.convertPayloadStringToString
|
|
103
101
|
export const convertFileToMrs = nativeBinding.convertFileToMrs
|
|
102
|
+
export const convertFileToBuffer = nativeBinding.convertFileToBuffer
|
|
103
|
+
export const convertFileToString = nativeBinding.convertFileToString
|
|
104
104
|
export const convertFileToPath = nativeBinding.convertFileToPath
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uruhalushia/rule-converter-napi",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Node.js bindings for converting mihomo, sing-box, Egern, and generic rule sets.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -51,17 +51,17 @@
|
|
|
51
51
|
"@napi-rs/cli": "^3"
|
|
52
52
|
},
|
|
53
53
|
"optionalDependencies": {
|
|
54
|
-
"@uruhalushia/rule-converter-napi-linux-x64-gnu": "0.1.
|
|
55
|
-
"@uruhalushia/rule-converter-napi-linux-arm64-gnu": "0.1.
|
|
56
|
-
"@uruhalushia/rule-converter-napi-linux-riscv64-gnu": "0.1.
|
|
57
|
-
"@uruhalushia/rule-converter-napi-linux-loong64-gnu": "0.1.
|
|
58
|
-
"@uruhalushia/rule-converter-napi-linux-x64-musl": "0.1.
|
|
59
|
-
"@uruhalushia/rule-converter-napi-linux-arm64-musl": "0.1.
|
|
60
|
-
"@uruhalushia/rule-converter-napi-linux-riscv64-musl": "0.1.
|
|
61
|
-
"@uruhalushia/rule-converter-napi-linux-loong64-musl": "0.1.
|
|
62
|
-
"@uruhalushia/rule-converter-napi-darwin-x64": "0.1.
|
|
63
|
-
"@uruhalushia/rule-converter-napi-darwin-arm64": "0.1.
|
|
64
|
-
"@uruhalushia/rule-converter-napi-win32-x64-msvc": "0.1.
|
|
65
|
-
"@uruhalushia/rule-converter-napi-win32-arm64-msvc": "0.1.
|
|
54
|
+
"@uruhalushia/rule-converter-napi-linux-x64-gnu": "0.1.3",
|
|
55
|
+
"@uruhalushia/rule-converter-napi-linux-arm64-gnu": "0.1.3",
|
|
56
|
+
"@uruhalushia/rule-converter-napi-linux-riscv64-gnu": "0.1.3",
|
|
57
|
+
"@uruhalushia/rule-converter-napi-linux-loong64-gnu": "0.1.3",
|
|
58
|
+
"@uruhalushia/rule-converter-napi-linux-x64-musl": "0.1.3",
|
|
59
|
+
"@uruhalushia/rule-converter-napi-linux-arm64-musl": "0.1.3",
|
|
60
|
+
"@uruhalushia/rule-converter-napi-linux-riscv64-musl": "0.1.3",
|
|
61
|
+
"@uruhalushia/rule-converter-napi-linux-loong64-musl": "0.1.3",
|
|
62
|
+
"@uruhalushia/rule-converter-napi-darwin-x64": "0.1.3",
|
|
63
|
+
"@uruhalushia/rule-converter-napi-darwin-arm64": "0.1.3",
|
|
64
|
+
"@uruhalushia/rule-converter-napi-win32-x64-msvc": "0.1.3",
|
|
65
|
+
"@uruhalushia/rule-converter-napi-win32-arm64-msvc": "0.1.3"
|
|
66
66
|
}
|
|
67
67
|
}
|