Skip to content

Commit 1cc5222

Browse files
authored
docs: architecture overview (#1230)
* docs: architecture overview * fix
1 parent 8716a57 commit 1cc5222

File tree

5 files changed

+1537
-12
lines changed

5 files changed

+1537
-12
lines changed

website/docs/next-env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/// <reference types="next/image-types/global" />
33

44
// NOTE: This file should not be edited
5-
// see https://nextjs.org/docs/basic-features/typescript for more information.
5+
// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.

website/docs/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"@radix-ui/react-tabs": "^1.0.1",
1818
"amplitude-js": "^8.13.0",
1919
"codesandbox-theme-docs": "^2.0.2",
20+
"mdx-mermaid": "^2.0.2",
21+
"mermaid": "^11.4.0",
2022
"next": "^14.0.0",
2123
"nextra": "^2.0.1",
2224
"nextra-theme-docs": "^2.0.1",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Overview
2+
3+
import { Mermaid } from 'mdx-mermaid/Mermaid';
4+
5+
1. Sandpack React
6+
7+
- `<Sandpack />` component initializes
8+
- Manages React integration & UI state
9+
10+
2. Sandpack Clinet
11+
12+
- `loadSandpackClient()` selects appropriate client type (VM/Runtime/Static/Node)
13+
- Creates sandbox context from files, template, and dependencies
14+
15+
3. Bundler
16+
17+
- Processes sandbox setup
18+
- Handles code bundling
19+
- Returns updates to client
20+
21+
<br />
22+
23+
<Mermaid chart={`
24+
sequenceDiagram
25+
autonumber
26+
participant R as Sandpack-react
27+
participant C as Sandpack-client
28+
29+
Note left of R: <Sandpack />
30+
Note right of C: loadSandpackClient(...)
31+
Note right of C: Depending on the template/env<br/>loadSandpackClient will return a<br />vm, runtime, static or node client
32+
Note right of C: sandpack-client/clients/index.ts
33+
R->>+C: Get sandbox context
34+
Note over R,C: Based on files, template, and deps, <br/> will be create a SandpackContextInfo.
35+
Note right of R: getSandpackStateFromProps(...)
36+
37+
C->>-R: New client instance
38+
Note left of R: new SandpackClient()
39+
40+
create participant B as Bundler
41+
C->>+B: Dispatch sandbox setup
42+
B->>+C: Messages with updates
43+
Note left of R: Once it's done, show <br/>iframe with preview
44+
C->>-R: Done
45+
46+
C->>+R: Dispatch sandbox setup
47+
`} />
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { Mermaid } from 'mdx-mermaid/Mermaid';
2+
3+
# Private dependencies: technical specification
4+
5+
This document outlines the technical specification for enabling the use of private dependencies on CodeSandbox, specifically for Pro users. This feature leverages the existing infrastructure used for CodeSandbox's private package proxy to provide a secure and seamless experience for developers working with internal or private packages.
6+
7+
### Use cases
8+
9+
- **CodeSandbox proxy:** all secrets are stored on csb server, which will proxy internally and respond with package content. This is the safest way to deal with sensitive data once it doesn’t expose any secrets to the client;
10+
- **Companies with internal VPN**: given the nature of a VPN restriction, csb will respond with the registry token to the client, which then will fetch to the npm registry in the internal network.
11+
12+
## Technical Specification
13+
14+
1. Authentication Flow:
15+
- The Sandpack component initiates the authentication flow by redirecting the user to `codesandbox.io/auth/sandpack?team-id=<team-id>`.
16+
- The CodeSandbox authentication service verifies the team-id and provides a sandpack-secret token.
17+
- The sandpack-secret token is stored in a secure cookie with `SameSite=None` and `Secure=true` settings.
18+
19+
2. Trusted Domains:
20+
- After the authentication flow, Sandpack requests the list of trusted domains for the specified team-id from the CodeSandbox API.
21+
- The API returns the list of trusted domains, which Sandpack uses to set the appropriate Content Security Policy (CSP) headers for the iframe.
22+
23+
24+
3. Package Fetch:
25+
- With the sandpack-secret token available, Sandpack makes requests to the CodeSandbox API to fetch the private package data.
26+
- The API authenticates the request using the sandpack-secret token and retrieves the package payload from the internal npm registry.
27+
- The API handles token renewal as necessary to ensure seamless access to the private package.
28+
29+
<br />
30+
31+
<Mermaid chart={`sequenceDiagram
32+
participant U as mydomain.com
33+
participant S as teamid.sandpack.codesandbox.io
34+
participant C as codesandbox.io
35+
participant A as API
36+
37+
Note over S: <Sandpack teamId="" />
38+
39+
alt Pop-up authentication
40+
Note over U: 1st step
41+
S->>+C: Sign-in [team-id]
42+
C->>-A: GET /auth/sandpack [team-id]
43+
Note over A: website-url match team config?
44+
A->>+C: sandpack-token
45+
C->>-S: sandpack-token
46+
Note over S: Cookie SameSite=None Secure=true
47+
end
48+
49+
Note over S: Refresh iframe to use the new cookie
50+
51+
alt Worker
52+
Note over U: 2nd step
53+
U --> S: iframe src: teamid.sandpack.codesandbox.io
54+
S ->>+A: GET /trusted-domains/:team-id
55+
A ->>-S: List of trusted domains
56+
S ->> U: CSP: frame-ancestors
57+
end
58+
59+
Note over U: At this point, this domain is a trusted one
60+
61+
alt Authenticated connection
62+
Note over U: 3rd step: run sandbox
63+
U->>+S: Run this sandbox
64+
S->>+A: GET /[team-id]/npm_registry
65+
A->>-S: Registry configuration
66+
S->>+A: GET /[team-id]/npm_registry/[package]
67+
Note over A: Make sure it renews the token as need
68+
A->>-S: Package payload
69+
end`} />
70+
71+
### Worker Technical Specification
72+
73+
<br />
74+
75+
<Mermaid chart={`
76+
sequenceDiagram
77+
participant U as mydomain.com
78+
participant S as teamid-2-0-0-sandpack.codesandbox.io
79+
participant A as API
80+
81+
U --> S: iframe src: teamid.sandpack.codesandbox.io
82+
S ->>+A: GET /trusted-domains/:team-id
83+
A ->>-S: List of trusted domains
84+
S ->> U: CSP: frame-ancestors
85+
`} />

0 commit comments

Comments
 (0)