Skip to content

Commit 89babdb

Browse files
authored
Merge branch 'chaitin:main' into main
2 parents 8e97305 + fbdb2c4 commit 89babdb

File tree

12 files changed

+625
-1
lines changed

12 files changed

+625
-1
lines changed

next.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ const nextConfig = withPlugins([withTM], {
2121
permanent: true,
2222
},
2323
];
24-
}: undefined,
24+
} : undefined,
2525
webpack: (config) => {
2626
config.resolve.fallback = { fs: false };
27+
config.experiments = {
28+
asyncWebAssembly: true,
29+
};
2730
return config;
2831
},
2932
});

public/pkg/cidr_aggregator.js

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
let wasm;
2+
3+
const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } );
4+
5+
if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); };
6+
7+
let cachedUint8ArrayMemory0 = null;
8+
9+
function getUint8ArrayMemory0() {
10+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
11+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
12+
}
13+
return cachedUint8ArrayMemory0;
14+
}
15+
16+
function getStringFromWasm0(ptr, len) {
17+
ptr = ptr >>> 0;
18+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
19+
}
20+
21+
const heap = new Array(128).fill(undefined);
22+
23+
heap.push(undefined, null, true, false);
24+
25+
let heap_next = heap.length;
26+
27+
function addHeapObject(obj) {
28+
if (heap_next === heap.length) heap.push(heap.length + 1);
29+
const idx = heap_next;
30+
heap_next = heap[idx];
31+
32+
heap[idx] = obj;
33+
return idx;
34+
}
35+
36+
let WASM_VECTOR_LEN = 0;
37+
38+
const cachedTextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder('utf-8') : { encode: () => { throw Error('TextEncoder not available') } } );
39+
40+
const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
41+
? function (arg, view) {
42+
return cachedTextEncoder.encodeInto(arg, view);
43+
}
44+
: function (arg, view) {
45+
const buf = cachedTextEncoder.encode(arg);
46+
view.set(buf);
47+
return {
48+
read: arg.length,
49+
written: buf.length
50+
};
51+
});
52+
53+
function passStringToWasm0(arg, malloc, realloc) {
54+
55+
if (realloc === undefined) {
56+
const buf = cachedTextEncoder.encode(arg);
57+
const ptr = malloc(buf.length, 1) >>> 0;
58+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
59+
WASM_VECTOR_LEN = buf.length;
60+
return ptr;
61+
}
62+
63+
let len = arg.length;
64+
let ptr = malloc(len, 1) >>> 0;
65+
66+
const mem = getUint8ArrayMemory0();
67+
68+
let offset = 0;
69+
70+
for (; offset < len; offset++) {
71+
const code = arg.charCodeAt(offset);
72+
if (code > 0x7F) break;
73+
mem[ptr + offset] = code;
74+
}
75+
76+
if (offset !== len) {
77+
if (offset !== 0) {
78+
arg = arg.slice(offset);
79+
}
80+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
81+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
82+
const ret = encodeString(arg, view);
83+
84+
offset += ret.written;
85+
ptr = realloc(ptr, len, offset, 1) >>> 0;
86+
}
87+
88+
WASM_VECTOR_LEN = offset;
89+
return ptr;
90+
}
91+
92+
function getObject(idx) { return heap[idx]; }
93+
94+
function dropObject(idx) {
95+
if (idx < 132) return;
96+
heap[idx] = heap_next;
97+
heap_next = idx;
98+
}
99+
100+
function takeObject(idx) {
101+
const ret = getObject(idx);
102+
dropObject(idx);
103+
return ret;
104+
}
105+
/**
106+
* @param {string} cidrs
107+
* @param {boolean} reverse
108+
* @param {boolean} exclude_reserved
109+
* @returns {any}
110+
*/
111+
export function aggregate(cidrs, reverse, exclude_reserved) {
112+
const ptr0 = passStringToWasm0(cidrs, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
113+
const len0 = WASM_VECTOR_LEN;
114+
const ret = wasm.aggregate(ptr0, len0, reverse, exclude_reserved);
115+
return takeObject(ret);
116+
}
117+
118+
async function __wbg_load(module, imports) {
119+
if (typeof Response === 'function' && module instanceof Response) {
120+
if (typeof WebAssembly.instantiateStreaming === 'function') {
121+
try {
122+
return await WebAssembly.instantiateStreaming(module, imports);
123+
124+
} catch (e) {
125+
if (module.headers.get('Content-Type') != 'application/wasm') {
126+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
127+
128+
} else {
129+
throw e;
130+
}
131+
}
132+
}
133+
134+
const bytes = await module.arrayBuffer();
135+
return await WebAssembly.instantiate(bytes, imports);
136+
137+
} else {
138+
const instance = await WebAssembly.instantiate(module, imports);
139+
140+
if (instance instanceof WebAssembly.Instance) {
141+
return { instance, module };
142+
143+
} else {
144+
return instance;
145+
}
146+
}
147+
}
148+
149+
function __wbg_get_imports() {
150+
const imports = {};
151+
imports.wbg = {};
152+
imports.wbg.__wbindgen_json_parse = function(arg0, arg1) {
153+
const ret = JSON.parse(getStringFromWasm0(arg0, arg1));
154+
return addHeapObject(ret);
155+
};
156+
157+
return imports;
158+
}
159+
160+
function __wbg_init_memory(imports, memory) {
161+
162+
}
163+
164+
function __wbg_finalize_init(instance, module) {
165+
wasm = instance.exports;
166+
__wbg_init.__wbindgen_wasm_module = module;
167+
cachedUint8ArrayMemory0 = null;
168+
169+
170+
171+
return wasm;
172+
}
173+
174+
function initSync(module) {
175+
if (wasm !== undefined) return wasm;
176+
177+
178+
if (typeof module !== 'undefined' && Object.getPrototypeOf(module) === Object.prototype)
179+
({module} = module)
180+
else
181+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
182+
183+
const imports = __wbg_get_imports();
184+
185+
__wbg_init_memory(imports);
186+
187+
if (!(module instanceof WebAssembly.Module)) {
188+
module = new WebAssembly.Module(module);
189+
}
190+
191+
const instance = new WebAssembly.Instance(module, imports);
192+
193+
return __wbg_finalize_init(instance, module);
194+
}
195+
196+
async function __wbg_init(module_or_path) {
197+
if (wasm !== undefined) return wasm;
198+
199+
200+
if (typeof module_or_path !== 'undefined' && Object.getPrototypeOf(module_or_path) === Object.prototype)
201+
({module_or_path} = module_or_path)
202+
else
203+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
204+
205+
if (typeof module_or_path === 'undefined') {
206+
module_or_path = new URL('cidr_aggregator_bg.wasm', import.meta.url);
207+
}
208+
const imports = __wbg_get_imports();
209+
210+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
211+
module_or_path = fetch(module_or_path);
212+
}
213+
214+
__wbg_init_memory(imports);
215+
216+
const { instance, module } = await __wbg_load(await module_or_path, imports);
217+
218+
return __wbg_finalize_init(instance, module);
219+
}
220+
221+
export { initSync };
222+
export default __wbg_init;

public/pkg/cidr_aggregator_bg.wasm

62.8 KB
Binary file not shown.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { countLines } from '@/utils';
2+
import { Box, TextField, Typography } from '@mui/material';
3+
import { useMemo } from 'react';
4+
import WarningFab from './WarningFab';
5+
6+
export default function InputEditor({
7+
input,
8+
setInput,
9+
output,
10+
}: {
11+
input: string;
12+
output: any;
13+
setInput: (value: string) => void;
14+
}) {
15+
return (
16+
<Box sx={{ position: 'relative' }}>
17+
<TextField
18+
id='input'
19+
label='输入'
20+
placeholder={`6.6.6.0/24\n192.168.1.0/24\n192.168.0.0/16`}
21+
multiline
22+
fullWidth
23+
autoFocus
24+
rows={10}
25+
inputProps={{ wrap: 'soft' }}
26+
value={input}
27+
onChange={(event) => setInput(event.target.value)}
28+
/>
29+
<Box>
30+
<Typography variant='caption' color='textSecondary'>
31+
行数: {useMemo(() => countLines(input), [input])}{' '}
32+
</Typography>
33+
</Box>
34+
<WarningFab invalidLines={output?.invalid} />
35+
</Box>
36+
);
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Button, Grid } from '@mui/material';
2+
import { ForwardedRef, forwardRef } from 'react';
3+
4+
function OptionsControl(
5+
{
6+
ipKind,
7+
toggleIpv4,
8+
toggleIpv6,
9+
bogonFilter,
10+
toggleReservedFilter,
11+
handleAggregate,
12+
}: {
13+
ipKind: string;
14+
toggleIpv4: () => void;
15+
toggleIpv6: () => void;
16+
bogonFilter?: string;
17+
toggleReservedFilter: () => void;
18+
handleAggregate: (reverse?: boolean) => void;
19+
},
20+
ref: ForwardedRef<any>
21+
) {
22+
return (
23+
<Grid container ref={ref} direction='row' justifyContent='flex-end'>
24+
<Grid item>
25+
<Button
26+
color='primary'
27+
size='small'
28+
variant='contained'
29+
onClick={() => handleAggregate()}
30+
>
31+
聚合
32+
</Button>
33+
</Grid>
34+
</Grid>
35+
);
36+
}
37+
38+
export default forwardRef(OptionsControl);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Box, TextField } from '@mui/material';
2+
import OutputStatusLine from './OutputStatusLine';
3+
4+
export default function OutputEditor({
5+
ipKind,
6+
output,
7+
}: {
8+
ipKind: string;
9+
output: any;
10+
}) {
11+
return (
12+
<Box position='relative'>
13+
{' '}
14+
<TextField
15+
id='input'
16+
label='输出'
17+
placeholder='No input'
18+
multiline
19+
fullWidth
20+
rows={10}
21+
inputProps={{ wrap: 'soft' }}
22+
value={[
23+
ipKind !== 'ipv6' && output?.v4?.ranges,
24+
ipKind !== 'ipv4' && output?.v6?.ranges,
25+
]
26+
.filter((v) => v)
27+
.join('\n')}
28+
/>
29+
<Box>
30+
<OutputStatusLine output={output} />
31+
</Box>
32+
</Box>
33+
);
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Grid, Typography } from '@mui/material';
2+
3+
function Partial({ name, status }: { name: string; status: any }) {
4+
return (
5+
<Typography variant='caption' color='textSecondary'>
6+
{name}: {status?.line_count_before ?? 0}
7+
<abbr title='行数'></abbr>/ {status?.address_count_before ?? '0'}
8+
&nbsp;&nbsp;➟&nbsp;&nbsp;
9+
<b>{status?.line_count_after ?? 0}</b>
10+
<abbr title='行数'></abbr> /{' '}
11+
<b>{status?.address_count_after ?? '0'} </b>
12+
</Typography>
13+
);
14+
}
15+
16+
export default function OutputStatusLine({ output }: { output: any }) {
17+
return (
18+
<Grid container direction='row' justifyContent='space-between'>
19+
<Grid item>
20+
<Partial name='IPv4' status={output?.v4} />
21+
</Grid>
22+
<Grid item>
23+
<Partial name='IPv6' status={output?.v6} />
24+
</Grid>
25+
</Grid>
26+
);
27+
}

0 commit comments

Comments
 (0)