merge ws/lazydetect
This commit is contained in:
commit
89b363af78
18 files changed
+16513
-1466
No files matched your search
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
+77
-44
@@ -78,6 +78,56 @@ end
|
||||
|
||||
local LATE: { [string]: number } = { br = 0.3 } -- archive/br: a trial decode of up to 256 KB
|
||||
|
||||
-- detection order: by the cost of a sniff (Registry.sniff: Sniffs.luau, Sniffs.luau reaching a codec, the format
|
||||
-- module), then registry order; the costly LATE sniffs last
|
||||
local order: { number }? = nil
|
||||
local byExt: { [string]: { number } } = {}
|
||||
local Sniffs: { [string]: (any) -> number }? = nil
|
||||
|
||||
local function sniffOrder(): { number }
|
||||
local o = order
|
||||
if o then
|
||||
return o
|
||||
end
|
||||
local list: { number } = {}
|
||||
for idx, e in Registry do
|
||||
table.insert(list, idx)
|
||||
for _, x in e.extensions do
|
||||
byExt[x] = byExt[x] or {}
|
||||
table.insert(byExt[x], idx)
|
||||
end
|
||||
end
|
||||
local function rank(idx: number): number
|
||||
local e = Registry[idx]
|
||||
return if LATE[e.name] then 3 else e.sniff
|
||||
end
|
||||
table.sort(list, function(a: number, b: number): boolean
|
||||
local ra, rb = rank(a), rank(b)
|
||||
if ra ~= rb then
|
||||
return ra < rb
|
||||
end
|
||||
return a < b
|
||||
end)
|
||||
order = list
|
||||
return list
|
||||
end
|
||||
|
||||
local function sniffer(e: Registry.Entry): ((any) -> number)?
|
||||
if e.sniff < 2 then
|
||||
local s = Sniffs
|
||||
if not s then
|
||||
s = require("@rosetta/Sniffs") :: any
|
||||
Sniffs = s
|
||||
end
|
||||
local f = (s :: { [string]: (any) -> number })[e.path]
|
||||
if f then
|
||||
return f
|
||||
end
|
||||
end
|
||||
local m = module(e)
|
||||
return if m and type(m.sniff) == "function" then m.sniff else nil
|
||||
end
|
||||
|
||||
local function detect(buf: buffer, name: string?, category: string?, format: string?): (Registry.Entry?, Err.Err?)
|
||||
if format then
|
||||
local e = find(format, category)
|
||||
@@ -87,52 +137,35 @@ local function detect(buf: buffer, name: string?, category: string?, format: str
|
||||
return e, nil
|
||||
end
|
||||
local ext = Input.extension(name)
|
||||
local list = sniffOrder()
|
||||
local hits = if ext then byExt[ext] else nil
|
||||
local best: Registry.Entry? = nil
|
||||
local bestScore, bestIdx = 0, 0
|
||||
-- sniffs score 0..1 (core/Sniff); the extension's format scoring 1 cannot be beaten. Formats with a costly sniff
|
||||
-- (LATE: the most they score) run last, and only when they could still win; ties go to registry order.
|
||||
local late: { number }? = nil
|
||||
for idx, e in Registry do
|
||||
if inCategory(e, category) and (e.caps.decode or e.caps.open) then
|
||||
local cap = LATE[e.name]
|
||||
if cap then
|
||||
late = late or {}
|
||||
table.insert(late :: { number }, idx)
|
||||
continue
|
||||
end
|
||||
local m = module(e)
|
||||
if m and m.sniff then
|
||||
local ok, score = pcall(m.sniff, Bytes.reader(buf))
|
||||
if ok and type(score) == "number" and score > 0 then
|
||||
local extHit = ext and table.find(e.extensions, ext)
|
||||
if extHit then
|
||||
score += 0.01 -- ties go to the extension
|
||||
end
|
||||
if score > bestScore then
|
||||
best, bestScore, bestIdx = e, score, idx
|
||||
if extHit and score >= 1.01 then
|
||||
return e, nil
|
||||
end
|
||||
end
|
||||
-- sniffs score 0..1 (core/Sniff), +0.01 for the extension's formats; the best wins, ties go to registry order. The
|
||||
-- extension's formats run first, and a sniff runs only while its cap (1, or LATE) could still win.
|
||||
for pass = 1, 2 do
|
||||
local ids = if pass == 1 then hits else list
|
||||
if ids then
|
||||
for _, idx in ids do
|
||||
if pass == 2 and bestScore > 1 then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if late then
|
||||
for _, idx in late do
|
||||
local e = Registry[idx]
|
||||
local extHit = ext and table.find(e.extensions, ext)
|
||||
local cap = LATE[e.name] + (if extHit then 0.01 else 0)
|
||||
if cap > bestScore or (cap == bestScore and idx < bestIdx) then
|
||||
local m = module(e)
|
||||
if m and m.sniff then
|
||||
local ok, score = pcall(m.sniff, Bytes.reader(buf))
|
||||
if ok and type(score) == "number" and score > 0 then
|
||||
if extHit then
|
||||
score += 0.01
|
||||
end
|
||||
if score > bestScore or (score == bestScore and idx < bestIdx) then
|
||||
best, bestScore, bestIdx = e, score, idx
|
||||
local e = Registry[idx]
|
||||
local extHit = pass == 1 or (hits ~= nil and table.find(hits, idx) ~= nil)
|
||||
if (pass == 1 or not extHit) and inCategory(e, category) and (e.caps.decode or e.caps.open) then
|
||||
local cap = (LATE[e.name] or 1) + (if extHit then 0.01 else 0)
|
||||
if cap > bestScore or (cap == bestScore and idx < bestIdx) then
|
||||
local sniff = sniffer(e)
|
||||
if sniff then
|
||||
local ok, score = pcall(sniff, Bytes.reader(buf))
|
||||
if ok and type(score) == "number" and score > 0 then
|
||||
if extHit then
|
||||
score += 0.01
|
||||
end
|
||||
if score > bestScore or (score == bestScore and idx < bestIdx) then
|
||||
best, bestScore, bestIdx = e, score, idx
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -696,7 +729,7 @@ function F.detect(src: any, opts: Options?): (string?, Err.Err?)
|
||||
return nil, err
|
||||
end
|
||||
-- a format may name the file's flavour more precisely (video/mkv: "webm" for DocType webm)
|
||||
local m = module(entry)
|
||||
local m = if entry.variant then module(entry) else nil
|
||||
if m and type(m.variant) == "function" then
|
||||
local ok, v = pcall(m.variant, Bytes.reader(buf))
|
||||
if ok and type(v) == "string" then
|
||||
|
||||
Reference in new issue
Block a user