@crvouga/mockingbird-preview 0.0.1 → 0.1.0
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.
- package/README.md +137 -80
- package/dist/{chunk-CFLI52WY.js → chunk-WAMTSUKW.js} +146 -108
- package/dist/chunk-WAMTSUKW.js.map +7 -0
- package/dist/dev-proxy.d.ts +176 -0
- package/dist/dev-proxy.js +15 -0
- package/dist/index.d.ts +59 -64
- package/dist/index.js +152 -162
- package/dist/index.js.map +4 -4
- package/package.json +5 -5
- package/dist/chunk-CFLI52WY.js.map +0 -7
- package/dist/sandbox-proxy.d.ts +0 -121
- package/dist/sandbox-proxy.js +0 -15
- /package/dist/{sandbox-proxy.js.map → dev-proxy.js.map} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} from "./chunk-
|
|
2
|
+
NON_PROD_PATTERNS,
|
|
3
|
+
createDevProxy,
|
|
4
|
+
devBaseUrlName,
|
|
5
|
+
devProviders,
|
|
6
|
+
devProxyWorker
|
|
7
|
+
} from "./chunk-WAMTSUKW.js";
|
|
8
8
|
|
|
9
9
|
// src/artifacts.ts
|
|
10
10
|
var legacyArtifact = (value) => {
|
|
@@ -592,122 +592,21 @@ function createCloudflarePreviews(options) {
|
|
|
592
592
|
return { deploy, list, remove };
|
|
593
593
|
}
|
|
594
594
|
|
|
595
|
-
// src/
|
|
596
|
-
function createPreviewHost(options) {
|
|
597
|
-
if (!options.artifact) throw new Error("Preview artifact identity is required");
|
|
598
|
-
let state = "starting";
|
|
599
|
-
let generation = 0;
|
|
600
|
-
let system;
|
|
601
|
-
let abort = new AbortController();
|
|
602
|
-
const clients = /* @__PURE__ */ new Set();
|
|
603
|
-
const start = async () => {
|
|
604
|
-
state = "starting";
|
|
605
|
-
try {
|
|
606
|
-
system = await options.boot({ generation, signal: abort.signal });
|
|
607
|
-
if (abort.signal.aborted) {
|
|
608
|
-
await system.dispose?.();
|
|
609
|
-
system = void 0;
|
|
610
|
-
return;
|
|
611
|
-
}
|
|
612
|
-
state = "ready";
|
|
613
|
-
} catch {
|
|
614
|
-
if (!abort.signal.aborted) state = "failed";
|
|
615
|
-
}
|
|
616
|
-
};
|
|
617
|
-
let settled = start();
|
|
618
|
-
const status = () => ({
|
|
619
|
-
state,
|
|
620
|
-
generation,
|
|
621
|
-
artifact: options.artifact,
|
|
622
|
-
clients: clients.size
|
|
623
|
-
});
|
|
624
|
-
const disconnect = () => {
|
|
625
|
-
for (const client of clients) client.dispose();
|
|
626
|
-
clients.clear();
|
|
627
|
-
};
|
|
628
|
-
const fetch2 = async (request) => {
|
|
629
|
-
await settled.catch(() => {
|
|
630
|
-
});
|
|
631
|
-
request.signal.throwIfAborted();
|
|
632
|
-
if (state !== "ready" || !system)
|
|
633
|
-
return Response.json({ error: "preview_not_ready", ...status() }, { status: 503 });
|
|
634
|
-
return system.fetch(request);
|
|
635
|
-
};
|
|
636
|
-
const connect = (port) => {
|
|
637
|
-
if (state === "disposed") throw new Error("Preview host is disposed");
|
|
638
|
-
const client = servePreviewPort(port, fetch2, {
|
|
639
|
-
...options.allow ? { allow: options.allow } : {},
|
|
640
|
-
onClose: () => clients.delete(client)
|
|
641
|
-
});
|
|
642
|
-
clients.add(client);
|
|
643
|
-
return () => {
|
|
644
|
-
clients.delete(client);
|
|
645
|
-
client.dispose();
|
|
646
|
-
};
|
|
647
|
-
};
|
|
648
|
-
const reset = () => {
|
|
649
|
-
if (state === "disposed") return Promise.reject(new Error("Preview host is disposed"));
|
|
650
|
-
disconnect();
|
|
651
|
-
abort.abort();
|
|
652
|
-
settled = settled.catch(() => {
|
|
653
|
-
}).then(async () => {
|
|
654
|
-
try {
|
|
655
|
-
await system?.dispose?.();
|
|
656
|
-
system = void 0;
|
|
657
|
-
if (state === "disposed") return;
|
|
658
|
-
generation++;
|
|
659
|
-
abort = new AbortController();
|
|
660
|
-
await start();
|
|
661
|
-
} catch (error) {
|
|
662
|
-
if (state !== "disposed") state = "failed";
|
|
663
|
-
throw error;
|
|
664
|
-
}
|
|
665
|
-
});
|
|
666
|
-
return settled;
|
|
667
|
-
};
|
|
668
|
-
const dispose = async () => {
|
|
669
|
-
state = "disposed";
|
|
670
|
-
disconnect();
|
|
671
|
-
abort.abort();
|
|
672
|
-
await settled.catch(() => {
|
|
673
|
-
});
|
|
674
|
-
try {
|
|
675
|
-
await system?.dispose?.();
|
|
676
|
-
system = void 0;
|
|
677
|
-
} finally {
|
|
678
|
-
state = "disposed";
|
|
679
|
-
}
|
|
680
|
-
};
|
|
681
|
-
return { connect, fetch: fetch2, reset, dispose, status, ready: () => settled };
|
|
682
|
-
}
|
|
683
|
-
function sharedPreviewWorkerName(identity) {
|
|
684
|
-
if (!identity.application || !identity.artifact || !Number.isSafeInteger(identity.generation) || identity.generation < 0)
|
|
685
|
-
throw new Error("Invalid preview worker identity");
|
|
686
|
-
return JSON.stringify([
|
|
687
|
-
"mockingbird-preview",
|
|
688
|
-
identity.application,
|
|
689
|
-
identity.artifact,
|
|
690
|
-
identity.generation
|
|
691
|
-
]);
|
|
692
|
-
}
|
|
693
|
-
|
|
694
|
-
// src/sandbox.ts
|
|
595
|
+
// src/dev.ts
|
|
695
596
|
var withoutTrailingSlashes = (value) => {
|
|
696
597
|
let end = value.length;
|
|
697
598
|
while (end > 0 && value.charCodeAt(end - 1) === 47) end--;
|
|
698
599
|
return value.slice(0, end);
|
|
699
600
|
};
|
|
700
|
-
var
|
|
701
|
-
|
|
702
|
-
modes: "mockingbird.sandbox.modes"
|
|
601
|
+
var DEV_STORAGE_KEYS = {
|
|
602
|
+
modes: "mockingbird.dev.modes"
|
|
703
603
|
};
|
|
704
|
-
var
|
|
705
|
-
none: "No
|
|
706
|
-
|
|
707
|
-
locked: "Enter the sandbox access key",
|
|
708
|
-
ready: "Sandbox access is granted: switch a service to send its requests to the vendor's sandbox"
|
|
604
|
+
var DEV_MESSAGES = {
|
|
605
|
+
none: "No dev proxy is configured for this preview",
|
|
606
|
+
ready: "Dev resources are available: switch a service to send its requests to its dev resource"
|
|
709
607
|
};
|
|
710
|
-
var unreachableMessage = (endpoint) => `The
|
|
608
|
+
var unreachableMessage = (endpoint) => `The dev proxy at ${endpoint} did not answer`;
|
|
609
|
+
var refusedMessage = (endpoint, reason) => `The dev proxy at ${endpoint} refused the request: ${reason}`;
|
|
711
610
|
var memoryStorage = () => {
|
|
712
611
|
const values = /* @__PURE__ */ new Map();
|
|
713
612
|
return {
|
|
@@ -759,14 +658,14 @@ var resolveProxyUrl = (proxyUrl) => {
|
|
|
759
658
|
url = origin ? new URL(proxyUrl, origin) : new URL(proxyUrl);
|
|
760
659
|
} catch {
|
|
761
660
|
throw new Error(
|
|
762
|
-
`
|
|
661
|
+
`dev proxyUrl ${JSON.stringify(proxyUrl)} must be absolute outside a browser page`
|
|
763
662
|
);
|
|
764
663
|
}
|
|
765
664
|
url.search = "";
|
|
766
665
|
url.hash = "";
|
|
767
666
|
return withoutTrailingSlashes(url.toString());
|
|
768
667
|
};
|
|
769
|
-
var NO_PROXY_BASE = "https://
|
|
668
|
+
var NO_PROXY_BASE = "https://dev-proxy.invalid";
|
|
770
669
|
var errorMessage = async (response, fallback) => {
|
|
771
670
|
try {
|
|
772
671
|
const body = await response.json();
|
|
@@ -783,7 +682,7 @@ var readMode = async (spec) => {
|
|
|
783
682
|
const response = await spec.admin(new Request("http://mockingbird.local/__admin/upstream"));
|
|
784
683
|
if (!response.ok) return "emulated";
|
|
785
684
|
const body = await response.json();
|
|
786
|
-
return body.mode === "forward" ? "
|
|
685
|
+
return body.mode === "forward" ? "dev" : "emulated";
|
|
787
686
|
} catch {
|
|
788
687
|
return "emulated";
|
|
789
688
|
}
|
|
@@ -792,20 +691,19 @@ var writeMode = (spec, mode) => spec.admin(
|
|
|
792
691
|
new Request("http://mockingbird.local/__admin/upstream", {
|
|
793
692
|
method: "PUT",
|
|
794
693
|
headers: { "content-type": "application/json" },
|
|
795
|
-
body: JSON.stringify({ mode: mode === "
|
|
694
|
+
body: JSON.stringify({ mode: mode === "dev" ? "forward" : "emulated" })
|
|
796
695
|
})
|
|
797
696
|
);
|
|
798
|
-
var
|
|
697
|
+
var createDevResources = (options = {}) => {
|
|
799
698
|
const endpoint = resolveProxyUrl(options.proxyUrl);
|
|
800
699
|
const storage = safeStorage(options.storage);
|
|
801
700
|
const send = options.fetch ?? ((request) => globalThis.fetch(request));
|
|
802
|
-
const accessKey = () => storage.getItem(SANDBOX_STORAGE_KEYS.accessKey) || null;
|
|
803
701
|
const storedModes = () => {
|
|
804
702
|
try {
|
|
805
|
-
const value = JSON.parse(storage.getItem(
|
|
703
|
+
const value = JSON.parse(storage.getItem(DEV_STORAGE_KEYS.modes) ?? "{}");
|
|
806
704
|
if (!value || typeof value !== "object" || Array.isArray(value)) return {};
|
|
807
705
|
return Object.fromEntries(
|
|
808
|
-
Object.entries(value).filter(([, mode]) => mode === "
|
|
706
|
+
Object.entries(value).filter(([, mode]) => mode === "dev")
|
|
809
707
|
);
|
|
810
708
|
} catch {
|
|
811
709
|
return {};
|
|
@@ -813,26 +711,19 @@ var createSandbox = (options = {}) => {
|
|
|
813
711
|
};
|
|
814
712
|
const storeMode = (id, mode) => {
|
|
815
713
|
const modes = storedModes();
|
|
816
|
-
if (mode === "
|
|
714
|
+
if (mode === "dev") modes[id] = "dev";
|
|
817
715
|
else delete modes[id];
|
|
818
|
-
storage.setItem(
|
|
819
|
-
};
|
|
820
|
-
const withKey = (request) => {
|
|
821
|
-
const out = new Request(request);
|
|
822
|
-
const key = accessKey();
|
|
823
|
-
if (key) out.headers.set(SANDBOX_KEY_HEADER, key);
|
|
824
|
-
else out.headers.delete(SANDBOX_KEY_HEADER);
|
|
825
|
-
return out;
|
|
716
|
+
storage.setItem(DEV_STORAGE_KEYS.modes, JSON.stringify(modes));
|
|
826
717
|
};
|
|
827
718
|
const upstream = (id) => ({
|
|
828
719
|
baseUrl: `${endpoint ?? NO_PROXY_BASE}/${encodeURIComponent(id)}`,
|
|
829
720
|
fetch: async (request) => {
|
|
830
721
|
if (!endpoint)
|
|
831
722
|
return Response.json(
|
|
832
|
-
{ error: { code: "
|
|
723
|
+
{ error: { code: "dev_unavailable", message: DEV_MESSAGES.none } },
|
|
833
724
|
{ status: 503 }
|
|
834
725
|
);
|
|
835
|
-
return send(
|
|
726
|
+
return send(request);
|
|
836
727
|
},
|
|
837
728
|
rewriteAuth: (request) => {
|
|
838
729
|
const out = new Request(request);
|
|
@@ -842,11 +733,18 @@ var createSandbox = (options = {}) => {
|
|
|
842
733
|
}
|
|
843
734
|
});
|
|
844
735
|
const readManifest = async () => {
|
|
845
|
-
if (!endpoint) return { status: { state: "none", message:
|
|
736
|
+
if (!endpoint) return { status: { state: "none", message: DEV_MESSAGES.none } };
|
|
846
737
|
let manifest;
|
|
847
738
|
try {
|
|
848
|
-
const response = await send(
|
|
849
|
-
if (!response.ok)
|
|
739
|
+
const response = await send(new Request(`${endpoint}/`));
|
|
740
|
+
if (!response.ok) {
|
|
741
|
+
const reason2 = await errorMessage(response, "");
|
|
742
|
+
if (reason2)
|
|
743
|
+
return {
|
|
744
|
+
status: { state: "unreachable", message: refusedMessage(endpoint, reason2), endpoint }
|
|
745
|
+
};
|
|
746
|
+
throw new Error(`status ${response.status}`);
|
|
747
|
+
}
|
|
850
748
|
manifest = await response.json();
|
|
851
749
|
if (!manifest || !Array.isArray(manifest.providers)) throw new Error("not a manifest");
|
|
852
750
|
} catch {
|
|
@@ -854,17 +752,16 @@ var createSandbox = (options = {}) => {
|
|
|
854
752
|
status: { state: "unreachable", message: unreachableMessage(endpoint), endpoint }
|
|
855
753
|
};
|
|
856
754
|
}
|
|
857
|
-
|
|
858
|
-
return { status, manifest };
|
|
755
|
+
return { status: { state: "ready", message: DEV_MESSAGES.ready, endpoint }, manifest };
|
|
859
756
|
};
|
|
860
757
|
const reason = (spec, { status, manifest }) => {
|
|
861
758
|
if (spec.unsupported) return spec.unsupported;
|
|
862
759
|
if (status.state !== "ready") return status.message;
|
|
863
760
|
const provider = manifest?.providers.find((candidate) => candidate.id === spec.id);
|
|
864
|
-
if (!provider) return `The
|
|
761
|
+
if (!provider) return `The dev proxy does not offer ${spec.label}`;
|
|
865
762
|
if (provider.missing.length > 0) return `Missing ${provider.missing.join(", ")} on the proxy`;
|
|
866
|
-
if (provider.
|
|
867
|
-
if (!provider.configured) return `${spec.label} is not configured on the
|
|
763
|
+
if (provider.blocked) return provider.blocked;
|
|
764
|
+
if (!provider.configured) return `${spec.label} is not configured on the dev proxy`;
|
|
868
765
|
return void 0;
|
|
869
766
|
};
|
|
870
767
|
const controls = (services) => {
|
|
@@ -894,9 +791,9 @@ var createSandbox = (options = {}) => {
|
|
|
894
791
|
const stored = storedModes();
|
|
895
792
|
await Promise.all(
|
|
896
793
|
services.map(async (spec) => {
|
|
897
|
-
if (stored[spec.id] !== "
|
|
898
|
-
if (await readMode(spec) === "
|
|
899
|
-
await apply(spec, "
|
|
794
|
+
if (stored[spec.id] !== "dev" || reason(spec, state) !== void 0) return;
|
|
795
|
+
if (await readMode(spec) === "dev") return;
|
|
796
|
+
await apply(spec, "dev").catch(() => {
|
|
900
797
|
});
|
|
901
798
|
})
|
|
902
799
|
);
|
|
@@ -913,28 +810,121 @@ var createSandbox = (options = {}) => {
|
|
|
913
810
|
};
|
|
914
811
|
})
|
|
915
812
|
);
|
|
916
|
-
return {
|
|
813
|
+
return { dev: state.status, services: rows };
|
|
917
814
|
},
|
|
918
815
|
setMode: async (id, mode) => {
|
|
919
816
|
const spec = find(id);
|
|
920
|
-
if (mode === "
|
|
817
|
+
if (mode === "dev") {
|
|
921
818
|
const why = reason(spec, await readManifest());
|
|
922
819
|
if (why !== void 0) throw new Error(why);
|
|
923
820
|
} else if (mode !== "emulated") throw new Error(`Unknown mode ${String(mode)}`);
|
|
924
821
|
await apply(spec, mode);
|
|
925
822
|
storeMode(id, mode);
|
|
926
|
-
}
|
|
927
|
-
setAccessKey: async (key) => {
|
|
928
|
-
const value = key?.trim();
|
|
929
|
-
if (value) storage.setItem(SANDBOX_STORAGE_KEYS.accessKey, value);
|
|
930
|
-
else storage.removeItem(SANDBOX_STORAGE_KEYS.accessKey);
|
|
931
|
-
},
|
|
932
|
-
hasAccessKey: () => accessKey() !== null
|
|
823
|
+
}
|
|
933
824
|
};
|
|
934
825
|
};
|
|
935
826
|
return { upstream, controls };
|
|
936
827
|
};
|
|
937
828
|
|
|
829
|
+
// src/host.ts
|
|
830
|
+
function createPreviewHost(options) {
|
|
831
|
+
if (!options.artifact) throw new Error("Preview artifact identity is required");
|
|
832
|
+
let state = "starting";
|
|
833
|
+
let generation = 0;
|
|
834
|
+
let system;
|
|
835
|
+
let abort = new AbortController();
|
|
836
|
+
const clients = /* @__PURE__ */ new Set();
|
|
837
|
+
const start = async () => {
|
|
838
|
+
state = "starting";
|
|
839
|
+
try {
|
|
840
|
+
system = await options.boot({ generation, signal: abort.signal });
|
|
841
|
+
if (abort.signal.aborted) {
|
|
842
|
+
await system.dispose?.();
|
|
843
|
+
system = void 0;
|
|
844
|
+
return;
|
|
845
|
+
}
|
|
846
|
+
state = "ready";
|
|
847
|
+
} catch {
|
|
848
|
+
if (!abort.signal.aborted) state = "failed";
|
|
849
|
+
}
|
|
850
|
+
};
|
|
851
|
+
let settled = start();
|
|
852
|
+
const status = () => ({
|
|
853
|
+
state,
|
|
854
|
+
generation,
|
|
855
|
+
artifact: options.artifact,
|
|
856
|
+
clients: clients.size
|
|
857
|
+
});
|
|
858
|
+
const disconnect = () => {
|
|
859
|
+
for (const client of clients) client.dispose();
|
|
860
|
+
clients.clear();
|
|
861
|
+
};
|
|
862
|
+
const fetch2 = async (request) => {
|
|
863
|
+
await settled.catch(() => {
|
|
864
|
+
});
|
|
865
|
+
request.signal.throwIfAborted();
|
|
866
|
+
if (state !== "ready" || !system)
|
|
867
|
+
return Response.json({ error: "preview_not_ready", ...status() }, { status: 503 });
|
|
868
|
+
return system.fetch(request);
|
|
869
|
+
};
|
|
870
|
+
const connect = (port) => {
|
|
871
|
+
if (state === "disposed") throw new Error("Preview host is disposed");
|
|
872
|
+
const client = servePreviewPort(port, fetch2, {
|
|
873
|
+
...options.allow ? { allow: options.allow } : {},
|
|
874
|
+
onClose: () => clients.delete(client)
|
|
875
|
+
});
|
|
876
|
+
clients.add(client);
|
|
877
|
+
return () => {
|
|
878
|
+
clients.delete(client);
|
|
879
|
+
client.dispose();
|
|
880
|
+
};
|
|
881
|
+
};
|
|
882
|
+
const reset = () => {
|
|
883
|
+
if (state === "disposed") return Promise.reject(new Error("Preview host is disposed"));
|
|
884
|
+
disconnect();
|
|
885
|
+
abort.abort();
|
|
886
|
+
settled = settled.catch(() => {
|
|
887
|
+
}).then(async () => {
|
|
888
|
+
try {
|
|
889
|
+
await system?.dispose?.();
|
|
890
|
+
system = void 0;
|
|
891
|
+
if (state === "disposed") return;
|
|
892
|
+
generation++;
|
|
893
|
+
abort = new AbortController();
|
|
894
|
+
await start();
|
|
895
|
+
} catch (error) {
|
|
896
|
+
if (state !== "disposed") state = "failed";
|
|
897
|
+
throw error;
|
|
898
|
+
}
|
|
899
|
+
});
|
|
900
|
+
return settled;
|
|
901
|
+
};
|
|
902
|
+
const dispose = async () => {
|
|
903
|
+
state = "disposed";
|
|
904
|
+
disconnect();
|
|
905
|
+
abort.abort();
|
|
906
|
+
await settled.catch(() => {
|
|
907
|
+
});
|
|
908
|
+
try {
|
|
909
|
+
await system?.dispose?.();
|
|
910
|
+
system = void 0;
|
|
911
|
+
} finally {
|
|
912
|
+
state = "disposed";
|
|
913
|
+
}
|
|
914
|
+
};
|
|
915
|
+
return { connect, fetch: fetch2, reset, dispose, status, ready: () => settled };
|
|
916
|
+
}
|
|
917
|
+
function sharedPreviewWorkerName(identity) {
|
|
918
|
+
if (!identity.application || !identity.artifact || !Number.isSafeInteger(identity.generation) || identity.generation < 0)
|
|
919
|
+
throw new Error("Invalid preview worker identity");
|
|
920
|
+
return JSON.stringify([
|
|
921
|
+
"mockingbird-preview",
|
|
922
|
+
identity.application,
|
|
923
|
+
identity.artifact,
|
|
924
|
+
identity.generation
|
|
925
|
+
]);
|
|
926
|
+
}
|
|
927
|
+
|
|
938
928
|
// src/worker.ts
|
|
939
929
|
function connectSharedPreviewWorker(url, options) {
|
|
940
930
|
const worker = new SharedWorker(url, { type: "module", name: sharedPreviewWorkerName(options) });
|
|
@@ -992,25 +982,25 @@ function installPreviewWorkerHost(host, scope) {
|
|
|
992
982
|
};
|
|
993
983
|
}
|
|
994
984
|
export {
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
985
|
+
DEV_MESSAGES,
|
|
986
|
+
DEV_STORAGE_KEYS,
|
|
987
|
+
NON_PROD_PATTERNS,
|
|
998
988
|
connectDedicatedPreviewWorker,
|
|
999
989
|
connectSharedPreviewWorker,
|
|
1000
990
|
createCloudflarePreviews,
|
|
991
|
+
createDevProxy,
|
|
992
|
+
createDevResources,
|
|
1001
993
|
createPreviewArtifact,
|
|
1002
994
|
createPreviewClient,
|
|
1003
995
|
createPreviewHost,
|
|
1004
|
-
|
|
1005
|
-
|
|
996
|
+
devBaseUrlName,
|
|
997
|
+
devProviders,
|
|
998
|
+
devProxyWorker,
|
|
1006
999
|
digestPreviewBytes,
|
|
1007
1000
|
installPreviewWorkerHost,
|
|
1008
1001
|
loadPreviewArtifact,
|
|
1009
1002
|
parsePreviewArtifact,
|
|
1010
1003
|
planPreviewSweep,
|
|
1011
|
-
safeEqual,
|
|
1012
|
-
sandboxProviders,
|
|
1013
|
-
sandboxProxyWorker,
|
|
1014
1004
|
selectPreviewAction,
|
|
1015
1005
|
servePreviewPort,
|
|
1016
1006
|
sharedPreviewWorkerName
|