Implement protocol adapters#29329
Conversation
| ); | ||
| userAssert( | ||
| !this.ssrTemplateHelper_.isEnabled(), | ||
| '[amp-list]: "amp-script" URIs cannot be used in SSR mode.' |
There was a problem hiding this comment.
Can you install amp-script (move past the error before this one) in a situation where SSR is enabled?
There was a problem hiding this comment.
Thats a good point. While this check exists for bind, <amp-script> is not valid within amp4email so it is likely unnecessary.
Although technically one could opt-in to SSR mode (and self-host the runtime to make their origin a whitelisted ssr-er) and then get past the earlier error.
🤷
There was a problem hiding this comment.
In the latter case, is there a reason they should not be able to opt into the amp-script protocol?
There was a problem hiding this comment.
Yep! Because in the SSR case, it is the viewer that actually does the fetch. We can't expect a viewer to know how to handle amp-script: scriptId.fnId
| expectAsyncConsoleError(errorMsg); | ||
| expect(list.layoutCallback()).to.eventually.throw(errorMsg); | ||
|
|
||
| element.setAttribute('src', 'amp-script:too.many.dots'); |
There was a problem hiding this comment.
For my learning: Is multiple dot call left out to limit scope?
There was a problem hiding this comment.
As written, the dot is meant to be a separator for ampscriptId and fnIdentifier. It isn't actually reaching into an object. For that reason only a single dot is valid.
Note: we could pick essentially any separator as long as the separator isn't a valid part of a js ident
|
|
||
| it('Waits for initialization to complete before returning', async () => { | ||
| element.setAttribute('script', 'local-script'); | ||
| const result = service.callFunction('local-script', 'fetchData'); |
There was a problem hiding this comment.
Is testing for bad inputs handled in worker dom? Also can that call an identifier with dots?
There was a problem hiding this comment.
I think I answer that in a response to an earlier question, let me know if I missed something!
#29329 (comment)
There was a problem hiding this comment.
I may have missed if there are test cases for this in worker dom, but I mean strings that are empty or otherwise not valid js variable names, i.e. 123 or anything containing odd symbols.
There was a problem hiding this comment.
We don't run the function name they pass directly into an eval() or anything like that. worker-dom exposes a function called exportFunction(string, handle) so that devs can specifically mark functions for export. As long as the string they pass in matches a string that was exported it'll work.
That is an interesting point that technically invalid identifiers can pass through this unharmed, I'm not convinced thats a bad thing.
dreamofabear
left a comment
There was a problem hiding this comment.
Can you add an e2e test in amp-script/test-e2e/ for this?
| * @param {string} fnIdentifier | ||
| * @return {Promise<*>} | ||
| */ | ||
| callFunction(ampScriptId, fnIdentifier) { |
There was a problem hiding this comment.
Why have this intermediary function on AmpScriptService at all?
There's only a single caller but if you want to avoid the boilerplate you could also hook into the internal actions API e.g. registerAction.
There was a problem hiding this comment.
This would actually solve a lot of interesting problems around args parsing so I'd love to make it work.
I'm not seeing a straightforward way to pipe through the return value though
There was a problem hiding this comment.
Ah yea, I guess executeAction could work but not sure it's a net win actually.
There was a problem hiding this comment.
Yeah. Calling executeAction would mean I'd still need to locate the correct element, so wouldn't help much here. A nice aspect is that if I registered the action, we'd get part (1) of this issue implemented for free: #24763
Also, I could imagine us moving in a direction eventually where devs pass a mixture of literal arguments/ amp-state arguments into the function. I skirt the issue by banning it entirely, but if we utilized the actions parser it could be easier to implement. This would become especially important if we wanted to make amp-script functions accessible from bind expressions.
Interop with bind expressions would let devs write a bound src with amp-state vars and have it automatically updated when the state variable is updated.
<amp-list [src]="amp-script:fns.fetchListData(filterArgs)">The current workaround requires chaining a list.refresh() action after the setState action.
Do you mind if I defer that to the next PR? |
|
cc @ampproject/wg-caching for the validator change |
honeybadgerdontcare
left a comment
There was a problem hiding this comment.
It's a nit, and I've been told to add a test for this, but...alphabetical....
In file /validator-amp-list.protoascii please keep the attributes alphabetical.
Sounds reasonable! I'll spin up a new PR to address this now. |
* cl/323700659 Revision bump for #29329 * cl/323797874 Revision bump for #29542 * cl/323858009 Revision bump for #29409 * cl/323889989 n/a * cl/324641097 Implements document level byte count limitation. * cl/324911894 Remove AMP Actions from Validator * cl/324915915 Revision bump for #29642 Co-authored-by: Greg Grothaus <greggrothaus@google.com> Co-authored-by: Amaltas Bohra <amaltas@google.com> Co-authored-by: honeybadgerdontcare <sedano@google.com>
* Implement protocol adapters * clean a bit * lint fixes * typefix * make example a promise * add amp-list tests * amp-script tests * goodbye nested ternaries you've served me well * round1 caro fixes * helpful message for devs * add unit test for single-item * fixed incorrect header * rebase to fix missing import * choumx feedback updates * make the tests pass + listen to Caros wisdom * lint fixes * rely on amp-script id, as well as getImpl throwing if extension absent * remove unused import
* cl/323700659 Revision bump for ampproject#29329 * cl/323797874 Revision bump for ampproject#29542 * cl/323858009 Revision bump for ampproject#29409 * cl/323889989 n/a * cl/324641097 Implements document level byte count limitation. * cl/324911894 Remove AMP Actions from Validator * cl/324915915 Revision bump for ampproject#29642 Co-authored-by: Greg Grothaus <greggrothaus@google.com> Co-authored-by: Amaltas Bohra <amaltas@google.com> Co-authored-by: honeybadgerdontcare <sedano@google.com>
summary
Implements Protocol adapters: #25719.
Introduces the concept of the
amp-scriptsrc for amp-list components.I picked this to be more in line with how initializing from
amp-stateworks, but if anyone feels strongly I'd beok with switching back to the more terse
fn:.Note that amp-bind is not required for this to work.
future work
cc @morsssss