@immediately-run/sandpack-client 2.19.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/README.md +26 -0
  2. package/dist/.ir-build-stamp.json +6 -0
  3. package/dist/base-DBh7xJX9.mjs +48 -0
  4. package/dist/base-DelKLlDk.js +50 -0
  5. package/dist/clients/base.d.ts +34 -0
  6. package/dist/clients/event-emitter.d.ts +10 -0
  7. package/dist/clients/iframe-factory.d.ts +27 -0
  8. package/dist/clients/index.d.ts +4 -0
  9. package/dist/clients/node/client.utils.d.ts +7 -0
  10. package/dist/clients/node/iframe.utils.d.ts +3 -0
  11. package/dist/clients/node/index.d.ts +49 -0
  12. package/dist/clients/node/index.js +631 -0
  13. package/dist/clients/node/index.mjs +629 -0
  14. package/dist/clients/node/inject-scripts/historyListener.d.ts +5 -0
  15. package/dist/clients/node/inject-scripts/index.d.ts +1 -0
  16. package/dist/clients/node/inject-scripts/resize.d.ts +5 -0
  17. package/dist/clients/node/taskManager.d.ts +20 -0
  18. package/dist/clients/node/types.d.ts +63 -0
  19. package/dist/clients/runtime/file-resolver-protocol.d.ts +17 -0
  20. package/dist/clients/runtime/iframe-protocol.d.ts +17 -0
  21. package/dist/clients/runtime/immutable-fetch-protocol.d.ts +39 -0
  22. package/dist/clients/runtime/index.d.ts +76 -0
  23. package/dist/clients/runtime/index.js +1046 -0
  24. package/dist/clients/runtime/index.mjs +1044 -0
  25. package/dist/clients/runtime/mime.d.ts +1 -0
  26. package/dist/clients/runtime/types.d.ts +129 -0
  27. package/dist/clients/runtime/utils.d.ts +8 -0
  28. package/dist/clients/static/index.d.ts +25 -0
  29. package/dist/clients/static/utils.d.ts +5 -0
  30. package/dist/consoleHook-DQVWjDRE.mjs +230 -0
  31. package/dist/consoleHook-znXctRzh.js +236 -0
  32. package/dist/fs/SandpackFS.d.ts +116 -0
  33. package/dist/iframe-factory-BcC-S_XQ.js +54 -0
  34. package/dist/iframe-factory-DybmkzJZ.mjs +51 -0
  35. package/dist/index--fILWAw8.js +210 -0
  36. package/dist/index-rbhm_KmF.mjs +208 -0
  37. package/dist/index.d.ts +4 -0
  38. package/dist/index.js +53 -0
  39. package/dist/index.mjs +40 -0
  40. package/dist/inject-scripts/consoleHook.d.ts +6 -0
  41. package/dist/types-BFONOA2L.mjs +531 -0
  42. package/dist/types-BIIEoWr6.js +534 -0
  43. package/dist/types.d.ts +348 -0
  44. package/dist/utils-BiVyytui.js +262 -0
  45. package/dist/utils-DG1HA4RZ.mjs +250 -0
  46. package/dist/utils.d.ts +18 -0
  47. package/dist/utils.js +13 -0
  48. package/dist/utils.mjs +2 -0
  49. package/package.json +82 -0
@@ -0,0 +1,63 @@
1
+ import type { WorkerStatusUpdate } from "@codesandbox/nodebox";
2
+ import type { BaseSandpackMessage, SandpackErrorMessage, SandpackLogLevel } from "../..";
3
+ type SandpackStandartMessages = {
4
+ type: "start";
5
+ firstLoad?: boolean;
6
+ } | {
7
+ type: "done";
8
+ compilatonError: boolean;
9
+ };
10
+ type SandpackBundlerMessages = {
11
+ type: "compile";
12
+ template?: string;
13
+ logLevel?: SandpackLogLevel;
14
+ } | ({
15
+ type: "action";
16
+ action: "show-error";
17
+ } & SandpackErrorMessage) | {
18
+ type: "action";
19
+ action: "notification";
20
+ notificationType: "error";
21
+ title: string;
22
+ };
23
+ type SandpackFSMessages = {
24
+ type: "fs/change";
25
+ path: string;
26
+ content: string;
27
+ } | {
28
+ type: "fs/remove";
29
+ path: string;
30
+ };
31
+ type SandpackURLsMessages = {
32
+ type: "urlchange";
33
+ url: string;
34
+ back: boolean;
35
+ forward: boolean;
36
+ } | {
37
+ type: "refresh";
38
+ } | {
39
+ type: "urlback";
40
+ } | {
41
+ type: "urlforward";
42
+ };
43
+ type SandpackShellMessages = {
44
+ type: "shell/restart";
45
+ } | {
46
+ type: "shell/openPreview";
47
+ } | {
48
+ type: "shell/progress";
49
+ data: WorkerStatusUpdate & {
50
+ command?: string;
51
+ };
52
+ };
53
+ export type SandpackNodeMessage = BaseSandpackMessage & (SandpackStandartMessages | SandpackURLsMessages | SandpackBundlerMessages | SandpackShellMessages | {
54
+ type: "connected";
55
+ } | {
56
+ type: "stdout";
57
+ payload: SandpackShellStdoutData;
58
+ } | SandpackFSMessages);
59
+ export interface SandpackShellStdoutData {
60
+ data?: string;
61
+ type?: "out" | "err";
62
+ }
63
+ export {};
@@ -0,0 +1,17 @@
1
+ /**
2
+ * This file is a copy of the resolver from the `codesandbox-api` package.
3
+ * We wanted to avoid to reference codesandbox-api because of the code that runs on load in the package.
4
+ * The plan is to take some time and refactor codesandbox-api into what it was supposed to be in the first place,
5
+ * an abstraction over the actions that can be dispatched between the bundler and the iframe.
6
+ */
7
+ import type { IFrameProtocol } from "./iframe-protocol";
8
+ import type { ProtocolRequestMessage } from "../../types";
9
+ export default class Protocol {
10
+ private type;
11
+ private handleMessage;
12
+ private protocol;
13
+ private _disposeMessageListener;
14
+ constructor(type: string, handleMessage: (message: ProtocolRequestMessage) => any, protocol: IFrameProtocol);
15
+ getTypeId(): string;
16
+ dispose(): void;
17
+ }
@@ -0,0 +1,17 @@
1
+ import type { ListenerFunction, SandpackMessage, UnsubscribeFunction } from "../../types";
2
+ export declare class IFrameProtocol {
3
+ private frameWindow;
4
+ private origin;
5
+ private globalListeners;
6
+ private globalListenersCount;
7
+ channelListeners: Record<number, ListenerFunction>;
8
+ private channelListenersCount;
9
+ readonly channelId: number;
10
+ constructor(iframe: HTMLIFrameElement, origin: string);
11
+ cleanup(): void;
12
+ register(port?: MessagePort, config?: Record<string, unknown>, babelPort?: MessagePort): void;
13
+ dispatch(message: SandpackMessage): void;
14
+ globalListen(listener: ListenerFunction): UnsubscribeFunction;
15
+ channelListen(listener: ListenerFunction): UnsubscribeFunction;
16
+ private eventListener;
17
+ }
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Parent-side fetch + cache for immutable, exact-versioned module URLs.
3
+ *
4
+ * The bundler iframe runs at an opaque origin (sandboxed without
5
+ * `allow-same-origin`), so it has no persistent storage of any kind — its own
6
+ * Cache API is unavailable. It therefore forwards immutable-URL fetches here
7
+ * over the iframe protocol (`protocol-immutable-fetch`), and the parent — a
8
+ * real origin — serves them cache-first from a persistent Cache API cache.
9
+ *
10
+ * Security: the handler only fetches URLs matching {@link IMMUTABLE_URL_ALLOWLIST}.
11
+ * The iframe runs untrusted code, and an unrestricted parent-side fetch proxy
12
+ * would let it read arbitrary cross-origin responses with the parent's network
13
+ * context (a CORS bypass). The allowlist mirrors the prefixes the bundler
14
+ * registers via `registerImmutableUrlPrefix` (see sandpack-bundler
15
+ * `src/utils/fetch.ts`) — keep the two in sync.
16
+ */
17
+ export interface ImmutableFetchResult {
18
+ status: number;
19
+ contentType: string;
20
+ /** Structured-cloned (copied) to the iframe in the protocol response. */
21
+ body: ArrayBuffer;
22
+ }
23
+ /**
24
+ * `protocol-immutable-fetch` handler: serve an allowlisted immutable URL,
25
+ * cache-first from the persistent parent-side cache.
26
+ *
27
+ * INTEGRITY-AWARE CACHING (cache-poisoning prevention). When the caller passes
28
+ * the expected SHA-384 (`integrity`), the cache is never allowed to serve or
29
+ * persist bytes that don't match it:
30
+ * - verify-on-read: a cache HIT whose bytes no longer match the expected hash
31
+ * (the host re-pinned the version under the cached bytes) is EVICTED and the
32
+ * URL refetched, so a stale/poisoned entry self-heals instead of failing the
33
+ * consumer forever.
34
+ * - verify-before-cache: freshly fetched bytes are only stored when they match.
35
+ * Mismatched bytes are returned (so the consumer's own check fails closed with
36
+ * a clear error) but NEVER cached, so a bad upstream window can't poison the
37
+ * cache. Without an `integrity` argument the URL is cached by URL as before.
38
+ */
39
+ export declare function handleImmutableFetch(url: unknown, integrity?: unknown): Promise<ImmutableFetchResult>;
@@ -0,0 +1,76 @@
1
+ import type { SandpackFS } from "../../fs/SandpackFS";
2
+ import type { BundlerState, ClientOptions, ListenerFunction, SandboxSetup, SandpackError, UnsubscribeFunction } from "../../types";
3
+ import { SandpackClient } from "../base";
4
+ import Protocol from "./file-resolver-protocol";
5
+ import { IFrameProtocol } from "./iframe-protocol";
6
+ import { type SandpackRuntimeMessage } from "./types";
7
+ export declare class SandpackRuntime extends SandpackClient {
8
+ fileResolverProtocol?: Protocol;
9
+ immutableFetchProtocol?: Protocol;
10
+ bundlerURL: string;
11
+ bundlerState?: BundlerState;
12
+ errors: SandpackError[];
13
+ selector: string;
14
+ element: Element;
15
+ unsubscribeGlobalListener: UnsubscribeFunction;
16
+ unsubscribeChannelListener: UnsubscribeFunction;
17
+ unsubscribeFsWatcher?: () => void;
18
+ iframeProtocol: IFrameProtocol;
19
+ fs: SandpackFS;
20
+ /** Parent-owned Babel transpiler worker, connected to the iframe by port. */
21
+ private babelWorker;
22
+ constructor(selector: string | HTMLIFrameElement, sandboxSetup: SandboxSetup, options?: ClientOptions);
23
+ private createBundlerURL;
24
+ /**
25
+ * Creates (or recreates) the parent-owned Babel transpiler worker and returns
26
+ * the `MessagePort` to transfer into the iframe. The iframe can no longer run
27
+ * its own worker — without `allow-same-origin` it has an opaque origin, and a
28
+ * same-origin worker script won't load there — so transpilation happens in
29
+ * this worker on the parent's real origin. Per-transform traffic then flows
30
+ * iframe<->worker directly over the entangled ports; the parent doesn't relay.
31
+ */
32
+ private createBabelWorkerPort;
33
+ private serviceWorkerHandshake;
34
+ private handleWorkerRequest;
35
+ setLocationURLIntoIFrame(): void;
36
+ destroy(): void;
37
+ updateOptions(options: ClientOptions): void;
38
+ /**
39
+ * Updates the local sandbox state. The bundler watches the shared filesystem
40
+ * itself, so there is nothing to push to the iframe here — a file mutation
41
+ * triggers a rebuild on the bundler side without a `compile` message. A
42
+ * template/environment change is handled by re-registering the client (see
43
+ * `useClient`'s `watchFileChanges` effect), which re-runs `computeInitConfig`.
44
+ */
45
+ updateSandbox(sandboxSetup?: SandboxSetup): void;
46
+ /**
47
+ * Computes the bundler's bootstrap config. Delivered once via the
48
+ * `register-frame` handshake (see the `initialized` handler) instead of a
49
+ * `compile` message, since the bundler now self-watches the filesystem.
50
+ */
51
+ private computeInitConfig;
52
+ /**
53
+ * Relays the set of changed file paths to the bundler so it can re-bundle.
54
+ * The bundler can't observe parent-side writes to the shared filesystem on
55
+ * its own: zenfs's `Port` backend doesn't forward watch events across the
56
+ * iframe boundary. This lightweight signal replaces the old per-change
57
+ * `compile` message — it carries only paths, not template/options/config.
58
+ */
59
+ notifyFilesChanged(paths: string[]): void;
60
+ dispatch(message: SandpackRuntimeMessage): void;
61
+ listen(listener: ListenerFunction): UnsubscribeFunction;
62
+ /**
63
+ * Get the URL of the contents of the current sandbox
64
+ */
65
+ getCodeSandboxURL(): Promise<{
66
+ sandboxId: string;
67
+ editorUrl: string;
68
+ embedUrl: string;
69
+ }>;
70
+ getTranspilerContext: () => Promise<Record<string, Record<string, unknown>>>;
71
+ getTranspiledFiles: () => Promise<Array<{
72
+ path: string;
73
+ code: string;
74
+ }>>;
75
+ private initializeElement;
76
+ }