Something went wrong. Try again.
Mirror: Best-effort discovery of the machine's local network using just Node.js dgram sockets
Something went wrong. Try again.
7.5 kB · 286 lines
JavaScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287import fs from 'node:fs/promises';import path from 'node:path/posix';import { fileURLToPath } from 'node:url';import { readFileSync } from 'node:fs';import { createRequire, isBuiltin } from 'node:module';
import * as prettier from 'prettier';import commonjs from '@rollup/plugin-commonjs';import resolve from '@rollup/plugin-node-resolve';import babel from '@rollup/plugin-babel';import terser from '@rollup/plugin-terser';import cjsCheck from 'rollup-plugin-cjs-check';import dts from 'rollup-plugin-dts';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const normalize = name => [] .concat(name) .join(' ') .replace(/[@\s/.]+/g, ' ') .trim() .replace(/\s+/, '-') .toLowerCase();
const extension = name => { if (/\.d.ts$/.test(name)) { return '.d.ts'; } else { return path.extname(name); }};
const meta = JSON.parse(readFileSync('package.json'));const name = normalize(meta.name);
const externalModules = [ ...Object.keys(meta.dependencies || {}), ...Object.keys(meta.peerDependencies || {}),];
const moduleRe = /^(?!node:|[.{1,2}\/])(@[\w.-]+\/)?[\w.-]+/;const externalRe = new RegExp(`^(${externalModules.join('|')})($|/)`);
const exports = {};for (const key in meta.exports) { const entry = meta.exports[key]; if (typeof entry === 'object' && !!entry.source) { const entryPath = normalize(key); const entryName = normalize([name, entryPath]); exports[entryName] = { path: entryPath, ...entry, }; }}
const externals = new Set();
const commonConfig = { input: Object.entries(exports).reduce((input, [exportName, entry]) => { input[exportName] = entry.source; return input; }, {}), onwarn: () => {}, external(id) { const isExternal = isBuiltin(id) || (externalModules.length && externalRe.test(id)); if (!isExternal && moduleRe.test(id)) externals.add(id); return isExternal; }, treeshake: { unknownGlobalSideEffects: false, tryCatchDeoptimization: false, moduleSideEffects: false, },};
const commonPlugins = [ resolve({ extensions: ['.mjs', '.js', '.ts'], mainFields: ['module', 'jsnext', 'main'], preferBuiltins: false, browser: true, }),
commonjs({ ignoreGlobal: true, include: /\/node_modules\//, }),];
const commonOutput = { dir: './', exports: 'auto', sourcemap: true, sourcemapExcludeSources: true, hoistTransitiveImports: false, indent: false, freeze: false, strict: false, generatedCode: { preset: 'es5', reservedNamesAsProps: false, objectShorthand: false, constBindings: false, },};
const outputPlugins = [ { name: 'outputPackageJsons', async writeBundle() { for (const key in exports) { const entry = exports[key]; if (entry.path) { const output = path.relative(entry.path, process.cwd()); const json = JSON.stringify({ name: key, private: true, version: '0.0.0', main: path.join(output, entry.require), types: path.join(output, entry.types), source: path.join(output, entry.source), exports: { '.': { types: path.join(output, entry.types), require: path.join(output, entry.require), source: path.join(output, entry.source), }, }, }, null, 2);
await fs.mkdir(entry.path, { recursive: true }); await fs.writeFile(path.join(entry.path, 'package.json'), json); } } }, },
{ name: 'outputBundledLicenses', async writeBundle() { const require = createRequire(import.meta.url); const rootLicense = path.join(__dirname, '../LICENSE.md'); const outputLicense = path.resolve('LICENSE.md'); if (rootLicense === outputLicense) return; const licenses = new Map(); for (const packageName of [...externals].sort()) { let license; let metaPath; let meta; try { metaPath = require.resolve(path.join(packageName, '/package.json')); meta = require(metaPath); } catch (_error) { continue; } const packagePath = path.dirname(metaPath); let licenseName = (await fs.readdir(packagePath).catch(() => [])) .find((name) => /^licen[sc]e/i.test(name)); if (!licenseName) { const match = /^SEE LICENSE IN (.*)/i.exec(meta.license || ''); licenseName = match ? match[1] : meta.license; } try { license = await fs.readFile(path.join(packagePath, licenseName), 'utf8'); } catch (_error) { license = meta.author ? `${licenseName}, Copyright (c) ${meta.author.name || meta.author}` : `${licenseName}, See license at: ${meta.repository.url || meta.repository}`; } licenses.set(packageName, license); } let output = (await fs.readFile(rootLicense, 'utf8')).trim(); for (const [packageName, licenseText] of licenses) output += `\n\n## ${packageName}\n\n${licenseText.trim()}`; await fs.writeFile(outputLicense, output); }, },
cjsCheck(),
terser({ warnings: true, ecma: 2015, keep_fnames: true, ie8: false, compress: { passes: 2, pure_getters: true, toplevel: true, booleans_as_integers: false, keep_fnames: true, keep_fargs: true, if_return: false, ie8: false, sequences: false, loops: false, conditionals: false, join_vars: false, reduce_vars: true, }, mangle: { module: true, keep_fnames: true, }, output: { beautify: true, braces: true, indent_level: 2, }, }),];
export default [ { ...commonConfig, plugins: [ ...commonPlugins, babel({ babelrc: false, babelHelpers: 'bundled', extensions: ['mjs', 'js', 'jsx', 'ts', 'tsx'], exclude: 'node_modules/**', presets: [], plugins: [ ['@babel/plugin-transform-typescript', { optimizeConstEnums: true, }], ], }), ], output: { ...commonOutput, format: 'cjs', esModule: true, externalLiveBindings: true, chunkFileNames(chunk) { return `dist/chunks/[name]-chunk${extension(chunk.name) || '.js'}`; }, entryFileNames(chunk) { return chunk.isEntry ? path.normalize(exports[chunk.name].require) : `dist/[name].js`; }, plugins: outputPlugins, }, },
{ ...commonConfig, plugins: [ ...commonPlugins, dts(), ], output: { ...commonOutput, sourcemap: false, format: 'dts', chunkFileNames(chunk) { return `dist/chunks/[name]-chunk${extension(chunk.name) || '.d.ts'}`; }, entryFileNames(chunk) { return chunk.isEntry ? path.normalize(exports[chunk.name].types) : `dist/[name].d.ts`; }, plugins: [ { renderChunk(code, chunk) { if (chunk.fileName.endsWith('d.ts')) { return prettier.format(code, { filepath: chunk.fileName, parser: 'typescript', singleQuote: true, tabWidth: 2, printWidth: 100, trailingComma: 'es5', }); } }, }, ], }, },];