Closed milahu closed 4 years ago
done. reduced bundle size by 95% : D
https://github.com/milahu/default-browser-name
/*
default-browser-name.js
size is 1380 byte
license is CC0-1.0
author is milahu
usage is
const
f = require('./default-browser-name.js'),
b = f(),
r = f({raw:1});
*/
const s=require('os'),
c=(s)=>require('child_process').execSync(
s,{maxBuffer:1/0, encoding:'utf-8'});
String.prototype.m1=function(e,f=''){
return[this.match(e)].map(r=>r?r[1]:f)[0]};
module.exports=(a={})=>([[
[/(linux|freebsd)/,()=>(
[c('xdg-mime query default text/html')]
.map(r=>a.raw?r:r.m1(/^.+?-(.+)-.+?/))[0])],
[/darwin/,()=> ( [
JSON.parse(c('plutil -convert json -o - '+
'~/Library/Preferences/com.apple.LaunchServices/'+
'com.apple.launchservices.secure.plist'))
.LSHandlers.filter(h => h.LSHandlerURLScheme == 'http')
.map(h => h.LSHandlerRoleAll).filter(Boolean)[0]
|| 'com.apple.Safari'].map(r=>a.raw?r:r.m1(/.+\.(.+?)$/)))],
[/win32/,()=>(
[s.release().slice(0,3)=='10.'
? ['HKEY_CURRENT_USER/SOFTWARE/Microsoft/Windows/Shell/'+
'Associations/URLAssociations/http/UserChoice',
'ProgId',/.+ {4}(.+?)\r/,/^(.+)(\.HTTP|URL|HTML)$/,'edge']
: ['HKCU/Software/Clients/StartMenuInternet',
'REG_SZ',/REG_SZ "(.+?)"/,/.+\\(.+?)(\.[^.]+)?$/,'ie']
].map(([p,k,e,f,d])=>c(
'reg query '+p.replace(/\//g,'\\')+' | findstr '+k).m1(e))
.map(r=>a.raw?r:r.m1(f,d))[0]
)],
].find(x=>s.platform().match(x[0]))
].map(r=>r?r[1]().trim():'')
.map(r=>a.raw?r:r.toLowerCase())[0]
)
Yeah, it could probably be improved. However, the priority was performance, not size. Spawning is very slow.
the current solution for darwin is horrible cos we need a 50 KB bundle to solve a problem that can be solved in like 1 KB
when on darwin, we can use platform-dependent
child_process.spawnSync
callsthis is just one sample solution
at first sight, the file in $x should not be converted back-and-forth between xml and binary format only once from binary to xml or something readable like JSON - yes, plutil can translate to json also avoid using temp-files
here is a draft version, i have no darwin machine to test = no warranty
but this is still a bad solution, the convert step should go
it should be even easier with
the standard API function is LSCopyDefaultApplicationURLForURL (this is used by electron to get default apps) but its closed source, so we cant use that to get the key path
whos got a darwin machine? what is the structure of
com.apple.LaunchServices
?edit: here is a sample output from
defaults read
output format is NeXTSTEP plist formatugly! and expensive to parse
so back to
plutil -convert json -o - com.apple.LaunchServices
then the minimal solution should be (using the logic from default-browser-id)that is 414 bytes of code : P
told ya, we can do this in a 1 KB bundle