Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage fixes #1035

Merged
merged 3 commits into from
Apr 1, 2021
Merged

storage fixes #1035

merged 3 commits into from
Apr 1, 2021

Conversation

samouri
Copy link
Member

@samouri samouri commented Mar 24, 2021

summary
Fixes two bugs discovered in ampproject/amphtml#32984. Note that both fixes are only 5 LOC. The rest are tests.

  1. When AMP.getState() is called with no args, the key was recorded as undefined in worker-thread but empty string in main-thread. The fix was to coerce falsy keys to an empty string in worker-thread as well. Without this the messages would never be matched properly and worker-thread always resolved the request with null.
  2. When the storageKey being requested is the 0th indexed string in StringContext, the StorageProcessor would always send over null. Essentially never a problem for a dom post-hydration, but could easily happen via nodom.

As a bonus change, I've also added 'GET_STORAGE' to the list of debuggable events.

@samouri samouri self-assigned this Mar 24, 2021
@samouri samouri force-pushed the fixampstate branch 2 times, most recently from b365e75 to f56b77d Compare March 24, 2021 17:43
@samouri samouri marked this pull request as ready for review March 24, 2021 17:47
src/main-thread/commands/storage.ts Show resolved Hide resolved
src/test/main-thread/commands/storage.test.ts Show resolved Hide resolved
src/worker-thread/amp/amp.ts Outdated Show resolved Hide resolved
1. StorageProcessor should accept a location key even if it has a
   0-index.
2. Adds printability to GET_LOCATION messages.
3. Even though TS forbids it, consumers can be JS. So lets coerce falsy
   calls to empty string.
@samouri samouri merged commit cdf65e9 into ampproject:main Apr 1, 2021
@samouri samouri deleted the fixampstate branch April 2, 2021 01:13
samouri added a commit that referenced this pull request Feb 22, 2022
The mutations array usually contains an index into StringContext for
value of key/value in the operation. Historically, "delete" was
modeled as 0th index into the array. Unfortunately, this didn't make
sense if the first SET op was genuinely referring to the 0th string
value (See #1035).

While this fixed the first get/set operation, it also meant that deletes
were no longer possible! This PR now models the delete operation as a -1
in the unsigned mutation array, corresponding to 2^16-1.
samouri added a commit that referenced this pull request Feb 28, 2022
* Storage: fix clear and removeItem

The mutations array usually contains an index into StringContext for
value of key/value in the operation. Historically, "delete" was
modeled as 0th index into the array. Unfortunately, this didn't make
sense if the first SET op was genuinely referring to the 0th string
value (See #1035).

While this fixed the first get/set operation, it also meant that deletes
were no longer possible! This PR now models the delete operation as a -1
in the unsigned mutation array, corresponding to 2^16-1.

* dont rely on overflow

* switch to special casing 0-value

* clean up test

* switch to isolating change within LocalStorage processor

* jridgewell found fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants