@phpsandbox/sdk 0.0.52 → 0.0.54

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.
@@ -35,6 +35,8 @@ var PHPSandbox = (() => {
35
35
  BeaconTimeoutError: () => BeaconTimeoutError,
36
36
  Client: () => Client,
37
37
  CommandError: () => CommandError,
38
+ DeploymentApi: () => DeploymentApi,
39
+ DeploymentInstance: () => DeploymentInstance,
38
40
  ErrorEvent: () => ErrorEvent,
39
41
  FileChangeFilter: () => FileChangeFilter,
40
42
  FileChangeType: () => FileChangeType,
@@ -45,6 +47,7 @@ var PHPSandbox = (() => {
45
47
  LspConnection: () => LspConnection,
46
48
  Navigator: () => Navigator,
47
49
  NotebookApi: () => NotebookApi,
50
+ NotebookDeploymentApi: () => NotebookDeploymentApi,
48
51
  NotebookInitError: () => NotebookInitError,
49
52
  NotebookInstance: () => NotebookInstance,
50
53
  NotebookMail: () => NotebookMail,
@@ -2905,15 +2908,15 @@ var PHPSandbox = (() => {
2905
2908
  };
2906
2909
  ReconnectingWebSocket3.prototype._getNextDelay = function() {
2907
2910
  var _a = this._options, _b = _a.reconnectionDelayGrowFactor, reconnectionDelayGrowFactor = _b === void 0 ? DEFAULT.reconnectionDelayGrowFactor : _b, _c = _a.minReconnectionDelay, minReconnectionDelay = _c === void 0 ? DEFAULT.minReconnectionDelay : _c, _d = _a.maxReconnectionDelay, maxReconnectionDelay = _d === void 0 ? DEFAULT.maxReconnectionDelay : _d;
2908
- var delay = 0;
2911
+ var delay2 = 0;
2909
2912
  if (this._retryCount > 0) {
2910
- delay = minReconnectionDelay * Math.pow(reconnectionDelayGrowFactor, this._retryCount - 1);
2911
- if (delay > maxReconnectionDelay) {
2912
- delay = maxReconnectionDelay;
2913
+ delay2 = minReconnectionDelay * Math.pow(reconnectionDelayGrowFactor, this._retryCount - 1);
2914
+ if (delay2 > maxReconnectionDelay) {
2915
+ delay2 = maxReconnectionDelay;
2913
2916
  }
2914
2917
  }
2915
- this._debug("next delay", delay);
2916
- return delay;
2918
+ this._debug("next delay", delay2);
2919
+ return delay2;
2917
2920
  };
2918
2921
  ReconnectingWebSocket3.prototype._wait = function() {
2919
2922
  var _this = this;
@@ -3882,10 +3885,10 @@ var PHPSandbox = (() => {
3882
3885
  return this.okra.invoke("git.log", { ref });
3883
3886
  }
3884
3887
  diff(input) {
3885
- return this.okra.invoke("git.diff", input);
3888
+ return this.okra.invoke("git.diff", this.normalizeRemoteInput(input));
3886
3889
  }
3887
3890
  merge(input) {
3888
- return this.okra.invoke("git.merge", input);
3891
+ return this.okra.invoke("git.merge", this.normalizeRemoteInput(input));
3889
3892
  }
3890
3893
  conflicts() {
3891
3894
  return this.okra.invoke("git.conflicts", {});
@@ -3917,6 +3920,35 @@ var PHPSandbox = (() => {
3917
3920
  status() {
3918
3921
  return this.okra.invoke("git.status", {});
3919
3922
  }
3923
+ normalizeRemoteInput(input) {
3924
+ if (input.notebookId === void 0) {
3925
+ return input;
3926
+ }
3927
+ const notebookId = input.notebookId.trim();
3928
+ if (!notebookId) {
3929
+ throw new Error("'notebookId' cannot be empty.");
3930
+ }
3931
+ if (input.url?.trim()) {
3932
+ throw new Error("Pass either 'url' or 'notebookId', not both.");
3933
+ }
3934
+ if (input.target?.trim()) {
3935
+ throw new Error("Pass either 'target' or 'notebookId', not both.");
3936
+ }
3937
+ const { notebookId: _notebookId, ...payload } = input;
3938
+ return {
3939
+ ...payload,
3940
+ url: this.gitUrlForNotebook(notebookId)
3941
+ };
3942
+ }
3943
+ gitUrlForNotebook(notebookId) {
3944
+ const url = new URL(this.okra.data.gitUrl);
3945
+ url.username = "";
3946
+ url.password = "";
3947
+ url.pathname = `/${encodeURIComponent(notebookId)}.git`;
3948
+ url.search = "";
3949
+ url.hash = "";
3950
+ return url.toString();
3951
+ }
3920
3952
  };
3921
3953
 
3922
3954
  // src/beacon/navigator.ts
@@ -4645,6 +4677,198 @@ var PHPSandbox = (() => {
4645
4677
  }
4646
4678
  }
4647
4679
 
4680
+ // src/deployments.ts
4681
+ var NotebookDeploymentApi = class {
4682
+ constructor(client) {
4683
+ this.client = client;
4684
+ }
4685
+ async create(id, input = {}) {
4686
+ const response = await this.client.post(`/notebook/${id}/deployments`, input);
4687
+ return new DeploymentInstance(response.data, this.client);
4688
+ }
4689
+ async list(id) {
4690
+ const response = await this.client.get(`/notebook/${id}/deployments`);
4691
+ return response.data;
4692
+ }
4693
+ async slugAvailability(id, slug) {
4694
+ const query = new URLSearchParams({ slug }).toString();
4695
+ const response = await this.client.get(
4696
+ `/notebook/${id}/deployments/slug-availability?${query}`
4697
+ );
4698
+ return response.data;
4699
+ }
4700
+ };
4701
+ var DeploymentApi = class {
4702
+ constructor(client) {
4703
+ this.client = client;
4704
+ }
4705
+ async get(id) {
4706
+ const response = await this.client.get(`/deployments/${id}`);
4707
+ return new DeploymentInstance(response.data, this.client);
4708
+ }
4709
+ async redeploy(id, input = {}) {
4710
+ const response = await this.client.post(`/deployments/${id}/redeploy`, input);
4711
+ return new DeploymentInstance(response.data, this.client);
4712
+ }
4713
+ async delete(id) {
4714
+ await this.client.delete(`/deployments/${id}`);
4715
+ }
4716
+ async setProtection(id, input) {
4717
+ const response = await this.client.put(`/deployments/${id}/protection`, input);
4718
+ return new DeploymentInstance(response.data, this.client);
4719
+ }
4720
+ async protection(id) {
4721
+ const response = await this.client.get(`/deployments/${id}/protection`);
4722
+ return response.data;
4723
+ }
4724
+ async createProtectionSession(id) {
4725
+ const response = await this.client.post(`/deployments/${id}/protection/session`);
4726
+ return response.data;
4727
+ }
4728
+ async disableProtection(id) {
4729
+ const response = await this.client.delete(`/deployments/${id}/protection`);
4730
+ return new DeploymentInstance(response.data, this.client);
4731
+ }
4732
+ async setFeedback(id, input) {
4733
+ const response = await this.client.put(`/deployments/${id}/feedback`, input);
4734
+ return new DeploymentInstance(response.data, this.client);
4735
+ }
4736
+ async listDomains(id) {
4737
+ const response = await this.client.get(
4738
+ `/deployments/${id}/domains`
4739
+ );
4740
+ return unwrapDeploymentDomainCollection(response.data);
4741
+ }
4742
+ async createDomain(id, input) {
4743
+ const response = await this.client.post(`/deployments/${id}/domains`, input);
4744
+ return response.data;
4745
+ }
4746
+ async refreshDomain(id, domainId) {
4747
+ const response = await this.client.post(`/deployments/${id}/domains/${domainId}/refresh`);
4748
+ return response.data;
4749
+ }
4750
+ async deleteDomain(id, domainId) {
4751
+ await this.client.delete(`/deployments/${id}/domains/${domainId}`);
4752
+ }
4753
+ async events(id) {
4754
+ return parseNdjsonStream(await this.client.stream(`/deployments/${id}/events/stream`));
4755
+ }
4756
+ async buildLogs(id, buildId) {
4757
+ return parseNdjsonStream(
4758
+ await this.client.stream(`/deployments/${id}/builds/${buildId}/logs/stream`)
4759
+ );
4760
+ }
4761
+ async logs(id) {
4762
+ return parseNdjsonStream(await this.client.stream(`/deployments/${id}/logs/stream`));
4763
+ }
4764
+ };
4765
+ var DeploymentInstance = class {
4766
+ constructor(data, client) {
4767
+ this.data = data;
4768
+ this.client = client;
4769
+ }
4770
+ refresh() {
4771
+ return this.client.deployments.get(this.data.id);
4772
+ }
4773
+ redeploy(input = {}) {
4774
+ return this.client.deployments.redeploy(this.data.id, input);
4775
+ }
4776
+ delete() {
4777
+ return this.client.deployments.delete(this.data.id);
4778
+ }
4779
+ async wait(options = {}) {
4780
+ const intervalMs = options.intervalMs ?? 1e3;
4781
+ const timeoutMs = options.timeoutMs ?? 3e5;
4782
+ const startedAt = Date.now();
4783
+ let current = this;
4784
+ while (!deploymentStatusIsTerminal(current.data.status)) {
4785
+ if (Date.now() - startedAt > timeoutMs) {
4786
+ throw new Error(`Timed out waiting for deployment ${this.data.id}`);
4787
+ }
4788
+ await delay(intervalMs);
4789
+ current = await current.refresh();
4790
+ }
4791
+ return current;
4792
+ }
4793
+ events() {
4794
+ return this.client.deployments.events(this.data.id);
4795
+ }
4796
+ buildLogs(buildId = this.data.latest_build?.id ?? "") {
4797
+ if (buildId === "") {
4798
+ return Promise.reject(new Error("Deployment has no latest build id"));
4799
+ }
4800
+ return this.client.deployments.buildLogs(this.data.id, buildId);
4801
+ }
4802
+ logs() {
4803
+ return this.client.deployments.logs(this.data.id);
4804
+ }
4805
+ setProtection(input) {
4806
+ return this.client.deployments.setProtection(this.data.id, input);
4807
+ }
4808
+ protection() {
4809
+ return this.client.deployments.protection(this.data.id);
4810
+ }
4811
+ createProtectionSession() {
4812
+ return this.client.deployments.createProtectionSession(this.data.id);
4813
+ }
4814
+ disableProtection() {
4815
+ return this.client.deployments.disableProtection(this.data.id);
4816
+ }
4817
+ setFeedback(input) {
4818
+ return this.client.deployments.setFeedback(this.data.id, input);
4819
+ }
4820
+ domains() {
4821
+ return this.client.deployments.listDomains(this.data.id);
4822
+ }
4823
+ addDomain(input) {
4824
+ return this.client.deployments.createDomain(this.data.id, input);
4825
+ }
4826
+ refreshDomain(domainId) {
4827
+ return this.client.deployments.refreshDomain(this.data.id, domainId);
4828
+ }
4829
+ deleteDomain(domainId) {
4830
+ return this.client.deployments.deleteDomain(this.data.id, domainId);
4831
+ }
4832
+ };
4833
+ function deploymentStatusIsTerminal(status) {
4834
+ return ["healthy", "failed", "stopped", "deleted", "delete_failed"].includes(status);
4835
+ }
4836
+ function delay(ms) {
4837
+ return new Promise((resolve) => setTimeout(resolve, ms));
4838
+ }
4839
+ function unwrapDeploymentDomainCollection(input) {
4840
+ if (Array.isArray(input)) {
4841
+ return input;
4842
+ }
4843
+ return Array.isArray(input.data) ? input.data : [];
4844
+ }
4845
+ function parseNdjsonStream(input) {
4846
+ const decoder = new TextDecoder();
4847
+ let buffer = "";
4848
+ return input.pipeThrough(
4849
+ new TransformStream({
4850
+ transform(chunk, controller) {
4851
+ buffer += decoder.decode(chunk, { stream: true });
4852
+ const lines = buffer.split("\n");
4853
+ buffer = lines.pop() ?? "";
4854
+ for (const line of lines) {
4855
+ const trimmed = line.trim();
4856
+ if (trimmed !== "") {
4857
+ controller.enqueue(JSON.parse(trimmed));
4858
+ }
4859
+ }
4860
+ },
4861
+ flush(controller) {
4862
+ buffer += decoder.decode();
4863
+ const trimmed = buffer.trim();
4864
+ if (trimmed !== "") {
4865
+ controller.enqueue(JSON.parse(trimmed));
4866
+ }
4867
+ }
4868
+ })
4869
+ );
4870
+ }
4871
+
4648
4872
  // src/index.ts
4649
4873
  function normalizeNotebookSecretInputs(input) {
4650
4874
  if (Array.isArray(input)) {
@@ -4665,18 +4889,42 @@ var PHPSandbox = (() => {
4665
4889
  };
4666
4890
  var ApiError = class extends Error {
4667
4891
  constructor(response, body) {
4668
- super(`PHPSandbox API Error: ${response.status} ${response.statusText} - ${body}`);
4892
+ const data = parseApiErrorBody(body);
4893
+ const apiMessage = apiErrorMessage(data, body);
4894
+ super(`PHPSandbox API Error: ${response.status} ${response.statusText}${apiMessage === "" ? "" : ` - ${apiMessage}`}`);
4669
4895
  this.response = response;
4670
4896
  this.body = body;
4671
4897
  this.status = response.status;
4898
+ this.data = data;
4899
+ this.apiMessage = apiMessage === "" ? void 0 : apiMessage;
4672
4900
  }
4673
4901
  };
4902
+ function parseApiErrorBody(body) {
4903
+ try {
4904
+ return JSON.parse(body);
4905
+ } catch {
4906
+ return null;
4907
+ }
4908
+ }
4909
+ function apiErrorMessage(data, fallback) {
4910
+ if (data !== null && typeof data === "object") {
4911
+ const record = data;
4912
+ if (typeof record.message === "string" && record.message.trim() !== "") {
4913
+ return record.message.trim();
4914
+ }
4915
+ if (record.error !== void 0) {
4916
+ return typeof record.error === "string" ? record.error.trim() : JSON.stringify(record.error);
4917
+ }
4918
+ }
4919
+ return fallback.trim();
4920
+ }
4674
4921
  var NotebookApi = class {
4675
4922
  constructor(client) {
4676
4923
  this.client = client;
4677
4924
  this.secrets = new NotebookSecretApi(client);
4678
4925
  this.preview = new NotebookPreviewApi(client);
4679
4926
  this.mail = new NotebookMailApi(client);
4927
+ this.deployments = new NotebookDeploymentApi(client);
4680
4928
  }
4681
4929
  async create(template, input = {}, init = true) {
4682
4930
  const response = await this.client.post("/notebook", { template, ...input });
@@ -4728,6 +4976,7 @@ var PHPSandbox = (() => {
4728
4976
  var Client = class {
4729
4977
  constructor(token, url = "https://api.phpsandbox.io/v1", options = {}) {
4730
4978
  this.fetch = globalThis.fetch;
4979
+ this.deployments = new DeploymentApi(this);
4731
4980
  this.notebook = new NotebookApi(this);
4732
4981
  this.options = Object.assign({ startClosed: true }, options);
4733
4982
  this.baseUrl = normalizeApiBaseUrl(url);
@@ -4752,6 +5001,21 @@ var PHPSandbox = (() => {
4752
5001
  put(path, body) {
4753
5002
  return this.makeRequest("PUT", path, { body: body === void 0 ? void 0 : JSON.stringify(body) });
4754
5003
  }
5004
+ async stream(path) {
5005
+ const response = await this.fetch(
5006
+ new Request(new URL(path.replace(/^\//, ""), this.baseUrl), {
5007
+ method: "GET",
5008
+ headers: this.headers
5009
+ })
5010
+ );
5011
+ if (!response.ok) {
5012
+ throw new ApiError(response, await response.text());
5013
+ }
5014
+ if (!response.body) {
5015
+ throw new Error("PHPSandbox API Error: response body is not readable");
5016
+ }
5017
+ return response.body;
5018
+ }
4755
5019
  async makeRequest(method, path, init) {
4756
5020
  const response = await this.fetch(
4757
5021
  new Request(new URL(path.replace(/^\//, ""), this.baseUrl), {
@@ -4898,6 +5162,9 @@ var PHPSandbox = (() => {
4898
5162
  fork(input = {}) {
4899
5163
  return this.client.notebook.fork(this.data.id, input);
4900
5164
  }
5165
+ deploy(input = {}) {
5166
+ return this.client.notebook.deployments.create(this.data.id, input);
5167
+ }
4901
5168
  gitCredentials() {
4902
5169
  return this.client.notebook.gitCredentials(this.data.id);
4903
5170
  }