Add scuttler prop instead of fixed Snow integration#475
Conversation
a8d8176 to
d07f04d
Compare
| } | ||
| snow(realm => performScuttleGlobalThis(realm, scuttleGlobalThisExceptions), globalRef) | ||
| scuttler(globalRef, { | ||
| scuttle: realm => performScuttleGlobalThis(realm, scuttleGlobalThisExceptions), |
There was a problem hiding this comment.
previously we had (cb, realm); here we have (realm, {scuttle:cb}).
I would prefer a more typical (realm, cb) and do away with wrapping the function in an object. If we ever need to introduce more extensibility, I would see options as a separate argument, like (realm, cb, {})
There was a problem hiding this comment.
(cb, realm) was proposed originally because the only "option" was to use snow, which accepts (cb, realm) because cb is required while realm is optional.
Now that we go with a more generic option that just snow, it makes sense to go by standard which is first realm and cb after.
I do agree with (realm, cb, {}) however, that's a fair suggestion.
There was a problem hiding this comment.
(You can see what I mean btw by visiting packages/browserify/examples/01-simple-js/index.html:5 on this PR)
| enabled: scuttleGlobalThisEnabled, | ||
| exceptions: scuttleGlobalThisExceptions, | ||
| recursive: scuttleGlobalThisRecursive, | ||
| scuttler: scuttleGlobalThisScuttler, |
There was a problem hiding this comment.
TBH I find this block (:79-83) a bit terse and tricky to parse and my eyes keep jumping back and forth especially now that scuttleGlobalThisScuttler is a field that is not "the final word" in this scope anymore.
I'd personally find it more readable to have a local options hash instead of local variables for each option.
Something like:
const scuttleOpts = scuttleGlobalThis === true
? scuttleGlobalThisDefaults
: {
...scuttleGlobalThis,
_scuttler: scuttleGlobalThis?.scuttler && globalRef[scuttleGlobalThis.scuttler] || scuttleGlobalThisDefaults._scuttler
}
And then in the following local scope we would refer to e.g. scuttleOpts.enabled instead of scuttleGlobalThisEnabled. We would also be calling scuttleOpts._scuttler, here prefixed with an underscore to distinguish the function-referencing key to the name-string referencing one in the options.
Might just be that I'm not fully accustomed to the new syntax here, just noting.
There was a problem hiding this comment.
I think it's really a matter of taste. I'm mostly trying to remain as align as possible with the rest of the codebase previous choices.
You can lookup } = in the codebase - this style is all over pretty much.
There was a problem hiding this comment.
Sure, I guess my main point is that with the changes in this PR, to answer the question "what is the effective scuttler", the reader will need walk up through lines 93,85,83,77,63 and make the connection that the falsiness of the empty string in defaults changes the outcome in the default scenario. (or walk down and keep this in mind at :87).
In the proposed alternative above, the logic for the specific field is kept at the same location.
There was a problem hiding this comment.
I accepted parts of your suggestion (left the part where an exception is thrown if a scuttler was specified but was not found) 6fe045a
|
i wasn't able to test our example at first ^ so updated our examples separately as follow-up in (both console errors from before unrelated to scuttler so looks good) |
This comment was marked as duplicate.
This comment was marked as duplicate.
|
added support restriction for |
|
added documentation PR #477 |
| if (scuttleGlobalThisRecursive) { | ||
| if (!globalRef.SNOW) { | ||
|
|
||
| const scuttleOpts = scuttleGlobalThis === true ? scuttleGlobalThisDefaults : scuttleGlobalThis |
There was a problem hiding this comment.
This will still potentially modify element in the exceptions property of the supplied scuttleGlobalThis down at https://github.com/LavaMoat/LavaMoat/pull/475/files#diff-cbec1016c903fe52b8d76860ebf3400e172b83a3374b8796bab149b3721d8064R113.
Hence preferable to consolidate the instantion and transformations of options with a pattern like
const scuttleOpts = {
// [...]
scuttlerFunc: scuttleGlobalThis?.scuttler ? ///....,
exceptions: scuttleGlobalThis?.exceptions?.map(e => scuttleExceptionToRegexp(e)) || scuttleGlobalThisDefaults.exceptions,
}
(the same input validation can still be done, just moved to happen before (or if there is reason to do so, after) the creation of scuttleOpts)
| if (scuttleGlobalThisRecursive) { | ||
| if (!globalRef.SNOW) { | ||
|
|
||
| const scuttleOpts = scuttleGlobalThis === true ? scuttleGlobalThisDefaults : scuttleGlobalThis |
There was a problem hiding this comment.
| // turn scuttleGlobalThis.exceptions regexes strings to actual regexes | ||
| for (let i = 0; i < scuttleGlobalThisExceptions.length; i++) { | ||
| const prop = scuttleGlobalThisExceptions[i] | ||
| for (let i = 0; i < scuttleOpts.exceptions.length; i++) { |
| enabled: true, | ||
| exceptions: [], | ||
| recursive: false, | ||
| scuttler: '', |
There was a problem hiding this comment.
this prop is expected to be a string, this is the default value of type:string (thought it would make sense to stick to one type if possible)
|
|
||
| function createKernel ({ projectRoot, lavamoatPolicy, canonicalNameMap, debugMode, statsMode, scuttleGlobalThis, scuttleGlobalThisExceptions }) { | ||
| if (scuttleGlobalThis?.scuttler) { | ||
| throw new Error('Lavamoat - "scuttleGlobalThis.scuttler" option is not supported for node environment.') |
There was a problem hiding this comment.
- we decided to allow using "scuttlerName" config option in node env
- since it's experimental, we decided to allow using it similar to how it's used for browser envs (by defining a prop on globalRef)
- later on we'd like to allow passing an actual reference instead of a string prop
- fix 6bd2c52
| } | ||
| snow = globalRef.SNOW | ||
| scuttleOpts.scuttlerFunc = globalRef[scuttleOpts.scuttler] | ||
| } |
There was a problem hiding this comment.
maybe should create a configuration obj that holds the scuttlerFunc instead of mutating the options obj
| enabled: true, | ||
| exceptions: [], | ||
| recursive: false, | ||
| scuttler: '', |
There was a problem hiding this comment.
should rename to scuttlerName or something
Co-authored-by: LeoTM <1881059+leotm@users.noreply.github.com>
3cec4e1 to
419e2a6
Compare
| } | ||
|
|
||
| const scuttleOpts = scuttleGlobalThis === true ? scuttleGlobalThisDefaults : scuttleGlobalThis | ||
| const scuttleOpts = Object.assign({}, scuttleGlobalThis === true ? scuttleGlobalThisDefaults : scuttleGlobalThis) |
There was a problem hiding this comment.
you're right, my bad, thank you
| } | ||
|
|
||
| const scuttleOpts = scuttleGlobalThis === true ? scuttleGlobalThisDefaults : scuttleGlobalThis | ||
| const scuttleOpts = Object.assign({}, scuttleGlobalThis === true ? scuttleGlobalThisDefaults : scuttleGlobalThis) |
There was a problem hiding this comment.
There was a problem hiding this comment.
you're right, my bad, thank you
Instead of
recursivepassscuttlerwhich will be a string for a reference on the global object for a scuttler function.Also, a demo of how to use this option to scuttle all realms using snow.
TODO