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

Realtime fixes: safari, timezones, and TIMED_OUT status #1585

Merged
merged 3 commits into from
Jan 7, 2025

Conversation

ericallam
Copy link
Member

@ericallam ericallam commented Jan 7, 2025

  • Adds a polyfill for ReadableStream[@@asyncIterator], which for some reason Safari still does not support
  • Adds missing TIMED_OUT TaskRunStatus case
  • Fixes realtime date coercion by forcing UTC (which they will always be).

Copy link

changeset-bot bot commented Jan 7, 2025

🦋 Changeset detected

Latest commit: a1a7531

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
@trigger.dev/core Patch
@trigger.dev/build Patch
trigger.dev Patch
@trigger.dev/sdk Patch
@internal/redis-worker Patch
@internal/zod-worker Patch
@trigger.dev/react-hooks Patch
@trigger.dev/rsc Patch
@trigger.dev/database Patch
@trigger.dev/otlp-importer Patch
references-nextjs-realtime Patch
@internal/testcontainers Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

coderabbitai bot commented Jan 7, 2025

Warning

Rate limit exceeded

@ericallam has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 26 minutes and 16 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 35493fc and a1a7531.

📒 Files selected for processing (4)
  • .changeset/poor-shirts-move.md (1 hunks)
  • .changeset/silent-trees-jump.md (1 hunks)
  • packages/core/src/v3/apiClient/runStream.ts (2 hunks)
  • packages/core/src/v3/schemas/api.ts (1 hunks)

Walkthrough

A patch has been introduced for the @trigger.dev/core package to address a Safari-specific compatibility issue with ReadableStream async iteration. The changes include adding a Safari detection utility function and implementing a polyfill to ensure proper functionality of async stream operations in the Safari browser environment.

Changes

File Change Summary
packages/core/src/v3/apiClient/runStream.ts - Added isSafari() function to detect Safari browser
- Implemented polyfill for ReadableStream.prototype[Symbol.asyncIterator] and ReadableStream.prototype.values
- Added case for "TIMED_OUT" in apiStatusFromRunStatus function
.changeset/poor-shirts-move.md Patch version update for @trigger.dev/core

Sequence Diagram

sequenceDiagram
    participant Browser
    participant RunStream
    participant ReadableStream
    
    Browser->>RunStream: Check browser type
    RunStream->>RunStream: Detect Safari
    alt Is Safari
        RunStream->>ReadableStream: Apply async iterator polyfill
    else Not Safari
        RunStream->>ReadableStream: Use native implementation
    end
Loading

Poem

🐰 In Safari's stream, a quirky tale unfolds
Where async iterators once were cold
A polyfill hops in with rabbit-like grace
Making streams dance at a smoother pace
Code leaps forward, no browser left behind! 🌈


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
packages/core/src/v3/apiClient/runStream.ts (2)

545-559: Consider using feature detection instead of User-Agent sniffing.

While the current implementation is defensive with proper environment checks, User-Agent sniffing can be fragile and may require maintenance as browser signatures evolve. Consider using feature detection:

 const isSafari = () => {
-  // Check if we're in a browser environment
-  if (
-    typeof window !== "undefined" &&
-    typeof navigator !== "undefined" &&
-    typeof navigator.userAgent === "string"
-  ) {
-    return (
-      /^((?!chrome|android).)*safari/i.test(navigator.userAgent) ||
-      /iPad|iPhone|iPod/.test(navigator.userAgent)
-    );
-  }
-  // If we're not in a browser environment, return false
-  return false;
+  return (
+    typeof window !== "undefined" &&
+    typeof ReadableStream === "function" &&
+    !ReadableStream.prototype[Symbol.asyncIterator]
+  );
 };

575-576: Document the reason for @ts-expect-error.

While the use of @ts-expect-error is valid here, it would be helpful to document why these type assertions are necessary.

Add comments explaining the type assertions:

-  // @ts-expect-error
+  // @ts-expect-error - TypeScript doesn't recognize dynamic additions to ReadableStream.prototype
   ReadableStream.prototype.values ??= function ({ preventCancel = false } = {}) {
   
-  // @ts-expect-error
+  // @ts-expect-error - TypeScript doesn't recognize dynamic additions to ReadableStream.prototype
   ReadableStream.prototype[Symbol.asyncIterator] ??= ReadableStream.prototype.values;

Also applies to: 607-608

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 668b34d and 511cfae.

📒 Files selected for processing (2)
  • .changeset/poor-shirts-move.md (1 hunks)
  • packages/core/src/v3/apiClient/runStream.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - npm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (buildjet-8vcpu-ubuntu-2204 - pnpm)
  • GitHub Check: units / 🧪 Unit Tests
  • GitHub Check: e2e / 🧪 CLI v3 tests (buildjet-8vcpu-ubuntu-2204 - npm)
  • GitHub Check: typecheck / typecheck
🔇 Additional comments (2)
packages/core/src/v3/apiClient/runStream.ts (1)

561-609: Implementation follows the spec correctly.

The polyfill implementation is robust with proper error handling, resource cleanup, and spec compliance. The code properly releases the reader lock in both success and error cases.

Let's verify that we're not overwriting any existing implementations:

✅ Verification successful

Polyfill implementation is safe and isolated

The implementation is the only ReadableStream polyfill in the codebase, properly guarded for Safari-only execution, and safely handles existing implementations through nullish coalescing.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if any other files in the codebase are implementing ReadableStream polyfills
# to avoid potential conflicts.

rg -l "ReadableStream.prototype\[Symbol.asyncIterator\]"

Length of output: 102

.changeset/poor-shirts-move.md (1)

1-5: LGTM!

The changeset correctly describes the patch and its purpose.

Copy link

pkg-pr-new bot commented Jan 7, 2025

@trigger.dev/react-hooks

npm i https://pkg.pr.new/triggerdotdev/trigger.dev/@trigger.dev/react-hooks@1585

trigger.dev

npm i https://pkg.pr.new/triggerdotdev/trigger.dev@1585

@trigger.dev/rsc

npm i https://pkg.pr.new/triggerdotdev/trigger.dev/@trigger.dev/rsc@1585

@trigger.dev/build

npm i https://pkg.pr.new/triggerdotdev/trigger.dev/@trigger.dev/build@1585

@trigger.dev/sdk

npm i https://pkg.pr.new/triggerdotdev/trigger.dev/@trigger.dev/sdk@1585

@trigger.dev/core

npm i https://pkg.pr.new/triggerdotdev/trigger.dev/@trigger.dev/core@1585

commit: a1a7531

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
packages/core/src/v3/apiClient/runStream.ts (2)

548-562: Consider more robust browser detection methods

While the current implementation works, User-Agent string detection has some limitations:

  1. It can be spoofed
  2. It may require updates as new devices/browsers are released
  3. Some browsers might modify their User-Agent strings

Consider using feature detection instead:

 const isSafari = () => {
-  // Check if we're in a browser environment
-  if (
-    typeof window !== "undefined" &&
-    typeof navigator !== "undefined" &&
-    typeof navigator.userAgent === "string"
-  ) {
-    return (
-      /^((?!chrome|android).)*safari/i.test(navigator.userAgent) ||
-      /iPad|iPhone|iPod/.test(navigator.userAgent)
-    );
-  }
-  // If we're not in a browser environment, return false
-  return false;
+  // Check if we're in a browser environment
+  if (typeof window === "undefined") return false;
+  
+  // Use feature detection to identify Safari
+  return !window.ReadableStream?.prototype?.[Symbol.asyncIterator];
 };

564-612: LGTM: Well-implemented polyfill with proper error handling

The polyfill implementation is solid:

  • Follows the spec correctly
  • Properly manages reader lifecycle
  • Includes comprehensive error handling
  • Well-documented with references

Consider adding type information for the error in the catch block:

-        } catch (e) {
+        } catch (e: unknown) {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 511cfae and 35493fc.

📒 Files selected for processing (1)
  • packages/core/src/v3/apiClient/runStream.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - npm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (buildjet-8vcpu-ubuntu-2204 - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (buildjet-8vcpu-ubuntu-2204 - npm)
  • GitHub Check: typecheck / typecheck
  • GitHub Check: units / 🧪 Unit Tests
🔇 Additional comments (2)
packages/core/src/v3/apiClient/runStream.ts (2)

531-533: LGTM: Status mapping addition is correct

The new "TIMED_OUT" status case follows the existing pattern and maintains consistency with other status mappings.


547-612: Verify Safari version compatibility

Let's verify which Safari versions require this polyfill to ensure we're not applying it unnecessarily to newer versions that might support the feature natively.

✅ Verification successful

Safari polyfill implementation verified as correct and necessary

The polyfill is properly implemented following the streams specification and is still required due to an open WebKit bug (#194379). The implementation safely detects Safari browsers and only applies the polyfill when needed, using nullish coalescing to avoid conflicts with native implementations.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check Safari versions that support ReadableStream[Symbol.asyncIterator]
# by searching for any version-specific checks or comments in the codebase

# Search for Safari version-related comments or checks
rg -i "safari.*version|webkit.*bug" -g '!{node_modules,dist,build}/*'

# Search for any existing browser compatibility checks
rg -i "browser.*compat|feature.*detect" -g '!{node_modules,dist,build}/*'

Length of output: 65804


Script:

#!/bin/bash
# Search for ReadableStream related code and version checks
rg -i "readablestream|safari.*version" -A 5 -B 5

# Search for any browser version detection patterns
rg -i "browser.*version|useragent.*version" -A 5 -B 5

Length of output: 103782

@ericallam ericallam changed the title Fix realtime safari bug Realtime fixes: safari, dates, and TIMED_OUT status Jan 7, 2025
@ericallam ericallam changed the title Realtime fixes: safari, dates, and TIMED_OUT status Realtime fixes: safari, timezones, and TIMED_OUT status Jan 7, 2025
@ericallam ericallam force-pushed the fix/realtime-safari branch from 1bd320b to a1a7531 Compare January 7, 2025 20:21
@ericallam ericallam merged commit 76a5ac2 into main Jan 7, 2025
8 of 9 checks passed
@ericallam ericallam deleted the fix/realtime-safari branch January 7, 2025 20:47
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.

1 participant