|
13 | 13 | * PERFORMANCE OF THIS SOFTWARE. |
14 | 14 | */ |
15 | 15 | /* eslint-disable @typescript-eslint/no-require-imports */ |
16 | | -const { fuzz: negotiateMediaType } = require('./negotiateMediaType.cjs'); |
17 | | -const { fuzz: parseAcceptHeader } = require('./parseAcceptHeader.cjs'); |
18 | | -const { fuzz: parseMediaType } = require('./parseMediaType.cjs'); |
19 | | - |
20 | | -function fuzz(buf) { |
21 | | - if (buf.length < 1) return; |
22 | | - |
23 | | - switch (buf[0] & 0b11000000) { |
24 | | - case 0b00000000: |
25 | | - negotiateMediaType(buf); |
26 | | - break; |
27 | | - case 0b01000000: |
28 | | - parseAcceptHeader(buf); |
29 | | - break; |
30 | | - case 0b10000000: |
31 | | - parseMediaType(buf); |
32 | | - break; |
| 16 | + |
| 17 | +const fuzzFactory = require('./fuzzFactory.cjs'); |
| 18 | + |
| 19 | +const { |
| 20 | + enableSet, |
| 21 | + isSetEnabled, |
| 22 | + enableStringIncludes, |
| 23 | + isStringIncludesEnabled, |
| 24 | + enableWeakMap, |
| 25 | + isWeakMapEnabled, |
| 26 | +} = (() => { |
| 27 | + const { Set, WeakMap } = globalThis; |
| 28 | + const { includes: String_includes } = String.prototype; |
| 29 | + |
| 30 | + let SetEnabled = true, |
| 31 | + StringIncludesEnabled = true, |
| 32 | + WeakMapEnabled = true; |
| 33 | + |
| 34 | + const enableSet = (v) => { |
| 35 | + SetEnabled = v; |
| 36 | + }; |
| 37 | + const isSetEnabled = () => SetEnabled; |
| 38 | + const enableStringIncludes = (v) => { |
| 39 | + StringIncludesEnabled = v; |
| 40 | + }; |
| 41 | + const isStringIncludesEnabled = () => StringIncludesEnabled; |
| 42 | + const enableWeakMap = (v) => { |
| 43 | + WeakMapEnabled = v; |
| 44 | + }; |
| 45 | + const isWeakMapEnabled = () => WeakMapEnabled; |
| 46 | + |
| 47 | + Object.defineProperties(globalThis, { |
| 48 | + Set: { |
| 49 | + get: () => { |
| 50 | + return SetEnabled ? Set : undefined; |
| 51 | + }, |
| 52 | + }, |
| 53 | + WeakMap: { |
| 54 | + get: () => { |
| 55 | + return WeakMapEnabled ? WeakMap : undefined; |
| 56 | + }, |
| 57 | + }, |
| 58 | + }); |
| 59 | + |
| 60 | + Object.defineProperties(String.prototype, { |
| 61 | + includes: { |
| 62 | + get: () => { |
| 63 | + return StringIncludesEnabled ? String_includes : undefined; |
| 64 | + }, |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + return { |
| 69 | + enableSet, |
| 70 | + isSetEnabled, |
| 71 | + enableStringIncludes, |
| 72 | + isStringIncludesEnabled, |
| 73 | + enableWeakMap, |
| 74 | + isWeakMapEnabled, |
| 75 | + }; |
| 76 | +})(); |
| 77 | + |
| 78 | +function hotRequire(id) { |
| 79 | + delete require.cache[require.resolve(id)]; |
| 80 | + |
| 81 | + const SetEnabled = isSetEnabled(); |
| 82 | + const WeakMapEnabled = isWeakMapEnabled(); |
| 83 | + const StringIncludesEnabled = isStringIncludesEnabled(); |
| 84 | + |
| 85 | + enableSet(true); |
| 86 | + enableWeakMap(true); |
| 87 | + enableStringIncludes(true); |
| 88 | + |
| 89 | + try { |
| 90 | + return require(id); |
| 91 | + } finally { |
| 92 | + enableSet(SetEnabled); |
| 93 | + enableWeakMap(WeakMapEnabled); |
| 94 | + enableStringIncludes(StringIncludesEnabled); |
33 | 95 | } |
34 | 96 | } |
35 | 97 |
|
| 98 | +const fuzz = (() => { |
| 99 | + const map = Object.create(null); |
| 100 | + |
| 101 | + const getFeatureKey = () => { |
| 102 | + return [isSetEnabled, isWeakMapEnabled, isStringIncludesEnabled] |
| 103 | + .map((v) => +!!v()) |
| 104 | + .join(''); |
| 105 | + }; |
| 106 | + |
| 107 | + const require_ = (id) => { |
| 108 | + const key = getFeatureKey(); |
| 109 | + if (!Object.hasOwn(map, key)) { |
| 110 | + map[key] = Object.create(null); |
| 111 | + } |
| 112 | + const resolved = require.resolve(id); |
| 113 | + if (!Object.hasOwn(map[key], resolved)) { |
| 114 | + map[key][resolved] = hotRequire(resolved); |
| 115 | + } |
| 116 | + |
| 117 | + return map[key][resolved]; |
| 118 | + }; |
| 119 | + |
| 120 | + return fuzzFactory(require_, (buf) => { |
| 121 | + enableSet(!(buf[0] & 0b0010_0000)); |
| 122 | + enableWeakMap(!(buf[0] & 0b0001_0000)); |
| 123 | + enableStringIncludes(!(buf[0] & 0b0000_1000)); |
| 124 | + }); |
| 125 | +})(); |
| 126 | + |
36 | 127 | module.exports = { fuzz }; |
0 commit comments