@yaks/sandbox 0.0.0-stage → 0.2.36

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 (74) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +400 -2
  3. package/_dist/bubblewrap.d.ts +38 -0
  4. package/_dist/bubblewrap.d.ts.map +1 -0
  5. package/_dist/cloudflare.d.ts +136 -0
  6. package/_dist/cloudflare.d.ts.map +1 -0
  7. package/_dist/configuration.d.ts +13 -0
  8. package/_dist/configuration.d.ts.map +1 -0
  9. package/_dist/docker.d.ts +15 -0
  10. package/_dist/docker.d.ts.map +1 -0
  11. package/_dist/effects.d.ts +6 -0
  12. package/_dist/effects.d.ts.map +1 -0
  13. package/_dist/lifecycle.d.ts +38 -0
  14. package/_dist/lifecycle.d.ts.map +1 -0
  15. package/_dist/mod.d.ts +4 -0
  16. package/_dist/mod.d.ts.map +1 -0
  17. package/_dist/provider.d.ts +33 -0
  18. package/_dist/provider.d.ts.map +1 -0
  19. package/_dist/pve.d.ts +17 -0
  20. package/_dist/pve.d.ts.map +1 -0
  21. package/_dist/recovery.d.ts +71 -0
  22. package/_dist/recovery.d.ts.map +1 -0
  23. package/_dist/tools.d.ts +4 -0
  24. package/_dist/tools.d.ts.map +1 -0
  25. package/_dist/vocab.d.ts +5 -0
  26. package/_dist/vocab.d.ts.map +1 -0
  27. package/archive.js +60 -0
  28. package/archive.js.map +1 -0
  29. package/archive.ts +49 -0
  30. package/bubblewrap.js +495 -0
  31. package/bubblewrap.js.map +1 -0
  32. package/bubblewrap.ts +555 -0
  33. package/cloudflare.js +434 -0
  34. package/cloudflare.js.map +1 -0
  35. package/cloudflare.ts +592 -0
  36. package/configuration.js +84 -0
  37. package/configuration.js.map +1 -0
  38. package/configuration.ts +120 -0
  39. package/deno.json +1 -0
  40. package/docker.js +149 -0
  41. package/docker.js.map +1 -0
  42. package/docker.ts +206 -0
  43. package/effects.js +86 -0
  44. package/effects.js.map +1 -0
  45. package/effects.ts +78 -0
  46. package/lifecycle.js +153 -0
  47. package/lifecycle.js.map +1 -0
  48. package/lifecycle.ts +207 -0
  49. package/mod.js +6 -0
  50. package/mod.js.map +1 -0
  51. package/mod.ts +7 -0
  52. package/package.json +64 -4
  53. package/provider.js +11 -0
  54. package/provider.js.map +1 -0
  55. package/provider.ts +44 -0
  56. package/pve.js +197 -0
  57. package/pve.js.map +1 -0
  58. package/pve.ts +259 -0
  59. package/recovery-script.js +187 -0
  60. package/recovery-script.js.map +1 -0
  61. package/recovery-script.ts +186 -0
  62. package/recovery.js +231 -0
  63. package/recovery.js.map +1 -0
  64. package/recovery.ts +317 -0
  65. package/sandbox-runner.js +218 -0
  66. package/sandbox-runner.js.map +1 -0
  67. package/sandbox-runner.ts +217 -0
  68. package/tools.js +42 -0
  69. package/tools.js.map +1 -0
  70. package/tools.ts +45 -0
  71. package/vocab.js +13 -0
  72. package/vocab.js.map +1 -0
  73. package/vocab.json +180 -0
  74. package/vocab.ts +11 -0
package/lifecycle.js ADDED
@@ -0,0 +1,153 @@
1
+ // Durable sandbox intentions and operations, independent of adapter SDKs.
2
+ import { mint, token } from "@yaks/graph";
3
+ import { exportPath } from "./provider.js";
4
+ let comp = (b, name)=>b[name] ?? {};
5
+ /** Resolve only the recorded placement, never a local fallback. */ export let sandboxBinding = (row, bindings)=>{
6
+ let name = String(comp(row, 'sandbox').provider ?? '');
7
+ let binding = bindings[name];
8
+ if (!binding) throw new Error(`Sandbox provider is not configured: ${name}`);
9
+ let machine = comp(row, 'workspace').machine;
10
+ if (machine !== binding.machine) {
11
+ throw new Error(`Sandbox provider ${name} does not serve the recorded computer`);
12
+ }
13
+ return binding;
14
+ };
15
+ export let sandboxRef = (row)=>({
16
+ id: row.entity.eid,
17
+ ...comp(row, 'sandbox').address ? {
18
+ address: String(comp(row, 'sandbox').address)
19
+ } : {}
20
+ });
21
+ export let sandboxRow = async (graph, id)=>{
22
+ let row = (await graph.get([
23
+ id
24
+ ]))[0];
25
+ if (!row?.sandbox || !row.workspace) {
26
+ throw new Error(`No sandbox workspace: ${id}`);
27
+ }
28
+ return row;
29
+ };
30
+ /** Persist this intention before provisioning. A caller may supply its durable
31
+ * call-derived id, making a repeated request name the same resource. */ export let sandboxNew = async (graph, bindings, request)=>{
32
+ let id = request.id ?? mint();
33
+ let existing = (await graph.get([
34
+ id
35
+ ]))[0];
36
+ if (existing) {
37
+ if (!existing.sandbox || existing.released) {
38
+ throw new Error(`Sandbox id cannot be reused: ${id}`);
39
+ }
40
+ let stored = comp(existing, 'sandbox');
41
+ if (stored.provider !== request.provider || comp(existing, 'workspace').machine !== request.machine || stored.from !== request.from || stored.image !== request.image) {
42
+ throw new Error(`Sandbox request changed: ${id}`);
43
+ }
44
+ sandboxBinding(existing, bindings);
45
+ if (existing.failed) throw new Error(`Sandbox request failed: ${id}`);
46
+ return existing;
47
+ }
48
+ let row = {
49
+ entity: {
50
+ eid: id
51
+ },
52
+ sandbox: {
53
+ provider: request.provider,
54
+ ...request.from ? {
55
+ from: request.from
56
+ } : {},
57
+ ...request.image ? {
58
+ image: request.image
59
+ } : {}
60
+ },
61
+ workspace: {
62
+ ...request.machine ? {
63
+ machine: request.machine
64
+ } : {}
65
+ },
66
+ pending: {}
67
+ };
68
+ let binding = sandboxBinding(row, bindings);
69
+ if (!binding.provider.request) {
70
+ throw new Error(`Sandbox provider cannot create: ${request.provider}`);
71
+ }
72
+ if (request.machine && !(await graph.get([
73
+ request.machine
74
+ ]))[0]?.machine) {
75
+ throw new Error(`No computer: ${request.machine}`);
76
+ }
77
+ await graph.apply([
78
+ {
79
+ ...row,
80
+ $was: {
81
+ sandbox: {
82
+ provider: null
83
+ }
84
+ }
85
+ }
86
+ ]);
87
+ return row;
88
+ };
89
+ /** Reconnect is never create. Pending, failed and released rows are not usable. */ export let sandboxConnect = async (graph, bindings, id)=>{
90
+ let row = await sandboxRow(graph, id);
91
+ if (row.released || row.pending || row.failed) {
92
+ throw new Error(`Sandbox is not ready: ${id}`);
93
+ }
94
+ let connection = await sandboxBinding(row, bindings).provider.connect(sandboxRef(row));
95
+ if (connection.root !== comp(row, 'workspace').root) {
96
+ throw new Error(`Sandbox workspace root changed: ${id}`);
97
+ }
98
+ return connection;
99
+ };
100
+ /** Called from a durable tool invocation. A release is recorded only after the
101
+ * provider confirms recoverable trash; repeated disposal never requests a new resource. */ export let sandboxDispose = async (graph, bindings, id)=>{
102
+ let row = await sandboxRow(graph, id);
103
+ if (row.released) return row;
104
+ if (row.pending) {
105
+ throw new Error(`Sandbox provisioning is still pending: ${id}`);
106
+ }
107
+ await sandboxBinding(row, bindings).provider.dispose(sandboxRef(row));
108
+ let released = {
109
+ entity: row.entity,
110
+ $was: {
111
+ sandbox: {
112
+ provider: token(comp(row, 'sandbox').provider),
113
+ address: comp(row, 'sandbox').address == null ? null : token(comp(row, 'sandbox').address)
114
+ },
115
+ workspace: {
116
+ machine: comp(row, 'workspace').machine == null ? null : token(comp(row, 'workspace').machine)
117
+ }
118
+ },
119
+ released: {},
120
+ pending: null,
121
+ failed: null
122
+ };
123
+ await graph.apply([
124
+ released
125
+ ]);
126
+ return released;
127
+ };
128
+ /** Restore the retained resource before clearing its trash receipt. */ export let sandboxRestore = async (graph, bindings, id)=>{
129
+ let row = await sandboxRow(graph, id);
130
+ if (!row.released) return row;
131
+ let provider = sandboxBinding(row, bindings).provider;
132
+ if (!provider.restore) throw new Error('Sandbox provider cannot restore');
133
+ await provider.restore(sandboxRef(row));
134
+ let restored = {
135
+ entity: row.entity,
136
+ released: null
137
+ };
138
+ await graph.apply([
139
+ restored
140
+ ]);
141
+ return restored;
142
+ };
143
+ /** Export only from an existing ready resource; relative paths are validated
144
+ * before an adapter is asked to read files. Bytes remain inside the caller's
145
+ * explicit export channel, never read through the host filesystem. */ export async function* sandboxExport(graph, bindings, id, paths) {
146
+ let selected = paths.map(exportPath);
147
+ let row = await sandboxRow(graph, id);
148
+ if (row.released || row.pending || row.failed) {
149
+ throw new Error(`Sandbox is not ready: ${id}`);
150
+ }
151
+ yield* sandboxBinding(row, bindings).provider.export(sandboxRef(row), selected);
152
+ }
153
+ //# sourceMappingURL=lifecycle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lifecycle.js","sources":["./lifecycle.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,SAA6C,IAAI,EAAE,KAAK,2BAAgC;AAOxF,SAAS,UAAU,wBAAuB;AA0B1C,IAAI,OAAO,CAAC,GAAW,OAAwB,CAAC,CAAC,KAAK,IAAI,CAAC;AAE3D,iEAAiE,GACjE,OAAO,IAAI,iBAAiB,CAC1B,KACA;EAEA,IAAI,OAAO,OAAO,KAAK,KAAK,WAAW,QAAQ,IAAI;EACnD,IAAI,UAAU,QAAQ,CAAC,KAAK;EAC5B,IAAI,CAAC,SAAS,MAAM,IAAI,MAAM,CAAC,oCAAoC,EAAE,MAAM;EAC3E,IAAI,UAAU,KAAK,KAAK,aAAa,OAAO;EAC5C,IAAI,YAAY,QAAQ,OAAO,EAAE;IAC/B,MAAM,IAAI,MACR,CAAC,iBAAiB,EAAE,KAAK,qCAAqC,CAAC;EAEnE;EACA,OAAO;AACT,EAAC;AACD,OAAO,IAAI,aAAa,CAAC,MAA4B,CAAC;IACpD,IAAI,IAAI,MAAM,CAAC,GAAG;IAClB,GAAI,KAAK,KAAK,WAAW,OAAO,GAC5B;MAAE,SAAS,OAAO,KAAK,KAAK,WAAW,OAAO;IAAE,IAChD,CAAC,CAAC;EACR,CAAC,EAAC;AACF,OAAO,IAAI,aAAa,OACtB,OACA;EAEA,IAAI,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC;IAAC;GAAG,CAAC,CAAC,CAAC,EAAE;EACpC,IAAI,CAAC,KAAK,WAAW,CAAC,IAAI,SAAS,EAAE;IACnC,MAAM,IAAI,MAAM,CAAC,sBAAsB,EAAE,IAAI;EAC/C;EACA,OAAO;AACT,EAAC;AAED;sEACsE,GACtE,OAAO,IAAI,aAAa,OACtB,OACA,UACA;EAEA,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OAAO;MAC1C;IACF;IACA,UAAU,CAAC;IACX,SAAS;IACT,QAAQ;EACV;EACA,MAAM,MAAM,KAAK,CAAC;IAAC;GAAS;EAC5B,OAAO;AACT,EAAC;AAED,qEAAqE,GACrE,OAAO,IAAI,iBAAiB,OAC1B,OACA,UACA;EAEA,IAAI,MAAM,MAAM,WAAW,OAAO;EAClC,IAAI,CAAC,IAAI,QAAQ,EAAE,OAAO;EAC1B,IAAI,WAAW,eAAe,KAAK,UAAU,QAAQ;EACrD,IAAI,CAAC,SAAS,OAAO,EAAE,MAAM,IAAI,MAAM;EACvC,MAAM,SAAS,OAAO,CAAC,WAAW;EAClC,IAAI,WAAmB;IAAE,QAAQ,IAAI,MAAM;IAAE,UAAU;EAAK;EAC5D,MAAM,MAAM,KAAK,CAAC;IAAC;GAAS;EAC5B,OAAO;AACT,EAAC;AAED;;oEAEoE,GACpE,OAAO,gBAAgB,cACrB,KAAyB,EACzB,QAAyB,EACzB,EAAU,EACV,KAAe;EAEf,IAAI,WAAW,MAAM,GAAG,CAAC;EACzB,IAAI,MAAM,MAAM,WAAW,OAAO;EAClC,IAAI,IAAI,QAAQ,IAAI,IAAI,OAAO,IAAI,IAAI,MAAM,EAAE;IAC7C,MAAM,IAAI,MAAM,CAAC,sBAAsB,EAAE,IAAI;EAC/C;EACA,OAAO,eAAe,KAAK,UAAU,QAAQ,CAAC,MAAM,CAClD,WAAW,MACX;AAEJ"}
package/lifecycle.ts ADDED
@@ -0,0 +1,207 @@
1
+ // Durable sandbox intentions and operations, independent of adapter SDKs.
2
+ import { type Bundle, type Comp, type Graph, mint, token } from '@yaks/graph'
3
+ import type {
4
+ SandboxConnection,
5
+ SandboxFile,
6
+ SandboxProvider,
7
+ SandboxRef,
8
+ } from './provider.js'
9
+ import { exportPath } from './provider.js'
10
+
11
+ /** A configured adapter and its explicit computer; omitted only for backends
12
+ * which cannot disclose placement. Never inferred from the executing host. */
13
+ export type SandboxBinding = { provider: SandboxProvider; machine?: string }
14
+ export type SandboxBindings = Record<string, SandboxBinding>
15
+ export type SandboxOptions = { providers: SandboxBindings }
16
+ /** Serializable adapter selection, separate from the module that connects it. */
17
+ export type Configuration = {
18
+ providers?: Record<
19
+ string,
20
+ SandboxBinding | {
21
+ use: string
22
+ machine?: string
23
+ with?: Record<string, unknown>
24
+ }
25
+ >
26
+ }
27
+
28
+ export type SandboxNew = {
29
+ id?: string
30
+ machine?: string
31
+ provider: string
32
+ from?: string
33
+ image?: string
34
+ }
35
+ let comp = (b: Bundle, name: string): Comp => (b[name] ?? {}) as Comp
36
+
37
+ /** Resolve only the recorded placement, never a local fallback. */
38
+ export let sandboxBinding = (
39
+ row: Bundle,
40
+ bindings: SandboxBindings,
41
+ ): SandboxBinding => {
42
+ let name = String(comp(row, 'sandbox').provider ?? '')
43
+ let binding = bindings[name]
44
+ if (!binding) throw new Error(`Sandbox provider is not configured: ${name}`)
45
+ let machine = comp(row, 'workspace').machine
46
+ if (machine !== binding.machine) {
47
+ throw new Error(
48
+ `Sandbox provider ${name} does not serve the recorded computer`,
49
+ )
50
+ }
51
+ return binding
52
+ }
53
+ export let sandboxRef = (row: Bundle): SandboxRef => ({
54
+ id: row.entity.eid,
55
+ ...(comp(row, 'sandbox').address
56
+ ? { address: String(comp(row, 'sandbox').address) }
57
+ : {}),
58
+ })
59
+ export let sandboxRow = async (
60
+ graph: Pick<Graph, 'get'>,
61
+ id: string,
62
+ ): Promise<Bundle> => {
63
+ let row = (await graph.get([id]))[0]
64
+ if (!row?.sandbox || !row.workspace) {
65
+ throw new Error(`No sandbox workspace: ${id}`)
66
+ }
67
+ return row
68
+ }
69
+
70
+ /** Persist this intention before provisioning. A caller may supply its durable
71
+ * call-derived id, making a repeated request name the same resource. */
72
+ export let sandboxNew = async (
73
+ graph: Pick<Graph, 'get' | 'apply'>,
74
+ bindings: SandboxBindings,
75
+ request: SandboxNew,
76
+ ): Promise<Bundle> => {
77
+ let id = request.id ?? mint()
78
+ let existing = (await graph.get([id]))[0]
79
+ if (existing) {
80
+ if (!existing.sandbox || existing.released) {
81
+ throw new Error(`Sandbox id cannot be reused: ${id}`)
82
+ }
83
+ let stored = comp(existing, 'sandbox')
84
+ if (
85
+ stored.provider !== request.provider ||
86
+ comp(existing, 'workspace').machine !== request.machine ||
87
+ stored.from !== request.from || stored.image !== request.image
88
+ ) {
89
+ throw new Error(`Sandbox request changed: ${id}`)
90
+ }
91
+ sandboxBinding(existing, bindings)
92
+ if (existing.failed) throw new Error(`Sandbox request failed: ${id}`)
93
+ return existing
94
+ }
95
+ let row: Bundle = {
96
+ entity: { eid: id },
97
+ sandbox: {
98
+ provider: request.provider,
99
+ ...(request.from ? { from: request.from } : {}),
100
+ ...(request.image ? { image: request.image } : {}),
101
+ },
102
+ workspace: {
103
+ ...(request.machine ? { machine: request.machine } : {}),
104
+ },
105
+ pending: {},
106
+ }
107
+ let binding = sandboxBinding(row, bindings)
108
+ if (!binding.provider.request) {
109
+ throw new Error(`Sandbox provider cannot create: ${request.provider}`)
110
+ }
111
+ if (request.machine && !(await graph.get([request.machine]))[0]?.machine) {
112
+ throw new Error(`No computer: ${request.machine}`)
113
+ }
114
+ await graph.apply([{ ...row, $was: { sandbox: { provider: null } } }])
115
+ return row
116
+ }
117
+
118
+ /** Reconnect is never create. Pending, failed and released rows are not usable. */
119
+ export let sandboxConnect = async (
120
+ graph: Pick<Graph, 'get'>,
121
+ bindings: SandboxBindings,
122
+ id: string,
123
+ ): Promise<SandboxConnection> => {
124
+ let row = await sandboxRow(graph, id)
125
+ if (row.released || row.pending || row.failed) {
126
+ throw new Error(`Sandbox is not ready: ${id}`)
127
+ }
128
+ let connection = await sandboxBinding(row, bindings).provider.connect(
129
+ sandboxRef(row),
130
+ )
131
+ if (connection.root !== comp(row, 'workspace').root) {
132
+ throw new Error(`Sandbox workspace root changed: ${id}`)
133
+ }
134
+ return connection
135
+ }
136
+
137
+ /** Called from a durable tool invocation. A release is recorded only after the
138
+ * provider confirms recoverable trash; repeated disposal never requests a new resource. */
139
+ export let sandboxDispose = async (
140
+ graph: Pick<Graph, 'get' | 'apply'>,
141
+ bindings: SandboxBindings,
142
+ id: string,
143
+ ): Promise<Bundle> => {
144
+ let row = await sandboxRow(graph, id)
145
+ if (row.released) return row
146
+ if (row.pending) {
147
+ throw new Error(`Sandbox provisioning is still pending: ${id}`)
148
+ }
149
+ await sandboxBinding(row, bindings).provider.dispose(sandboxRef(row))
150
+ let released: Bundle = {
151
+ entity: row.entity,
152
+ $was: {
153
+ sandbox: {
154
+ provider: token(comp(row, 'sandbox').provider),
155
+ address: comp(row, 'sandbox').address == null
156
+ ? null
157
+ : token(comp(row, 'sandbox').address),
158
+ },
159
+ workspace: {
160
+ machine: comp(row, 'workspace').machine == null
161
+ ? null
162
+ : token(comp(row, 'workspace').machine),
163
+ },
164
+ },
165
+ released: {},
166
+ pending: null,
167
+ failed: null,
168
+ }
169
+ await graph.apply([released])
170
+ return released
171
+ }
172
+
173
+ /** Restore the retained resource before clearing its trash receipt. */
174
+ export let sandboxRestore = async (
175
+ graph: Pick<Graph, 'get' | 'apply'>,
176
+ bindings: SandboxBindings,
177
+ id: string,
178
+ ): Promise<Bundle> => {
179
+ let row = await sandboxRow(graph, id)
180
+ if (!row.released) return row
181
+ let provider = sandboxBinding(row, bindings).provider
182
+ if (!provider.restore) throw new Error('Sandbox provider cannot restore')
183
+ await provider.restore(sandboxRef(row))
184
+ let restored: Bundle = { entity: row.entity, released: null }
185
+ await graph.apply([restored])
186
+ return restored
187
+ }
188
+
189
+ /** Export only from an existing ready resource; relative paths are validated
190
+ * before an adapter is asked to read files. Bytes remain inside the caller's
191
+ * explicit export channel, never read through the host filesystem. */
192
+ export async function* sandboxExport(
193
+ graph: Pick<Graph, 'get'>,
194
+ bindings: SandboxBindings,
195
+ id: string,
196
+ paths: string[],
197
+ ): AsyncIterable<SandboxFile> {
198
+ let selected = paths.map(exportPath)
199
+ let row = await sandboxRow(graph, id)
200
+ if (row.released || row.pending || row.failed) {
201
+ throw new Error(`Sandbox is not ready: ${id}`)
202
+ }
203
+ yield* sandboxBinding(row, bindings).provider.export(
204
+ sandboxRef(row),
205
+ selected,
206
+ )
207
+ }
package/mod.js ADDED
@@ -0,0 +1,6 @@
1
+ // Sandbox vocabulary. Provisioning adapters operate on explicit computers,
2
+ // independently of a session's default target.
3
+ export { sandboxDoc } from "./vocab.js";
4
+ export * from "./provider.js";
5
+ export * from "./lifecycle.js";
6
+ //# sourceMappingURL=mod.js.map
package/mod.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,+CAA+C;AAC/C,SAAS,UAAU,qBAAoB;AAEvC,8BAA6B;AAE7B,+BAA8B"}
package/mod.ts ADDED
@@ -0,0 +1,7 @@
1
+ // Sandbox vocabulary. Provisioning adapters operate on explicit computers,
2
+ // independently of a session's default target.
3
+ export { sandboxDoc } from './vocab.js'
4
+
5
+ export * from './provider.js'
6
+
7
+ export * from './lifecycle.js'
package/package.json CHANGED
@@ -1,6 +1,66 @@
1
1
  {
2
2
  "name": "@yaks/sandbox",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.36",
4
+ "homepage": "https://jsr.io/@yaks/sandbox",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./_dist/mod.d.ts",
9
+ "default": "./mod.js"
10
+ },
11
+ "./vocab": {
12
+ "types": "./_dist/vocab.d.ts",
13
+ "default": "./vocab.js"
14
+ },
15
+ "./docker": {
16
+ "types": "./_dist/docker.d.ts",
17
+ "default": "./docker.js"
18
+ },
19
+ "./cloudflare": {
20
+ "types": "./_dist/cloudflare.d.ts",
21
+ "default": "./cloudflare.js"
22
+ },
23
+ "./recovery": {
24
+ "types": "./_dist/recovery.d.ts",
25
+ "default": "./recovery.js"
26
+ },
27
+ "./bubblewrap": {
28
+ "types": "./_dist/bubblewrap.d.ts",
29
+ "default": "./bubblewrap.js"
30
+ },
31
+ "./pve": {
32
+ "types": "./_dist/pve.d.ts",
33
+ "default": "./pve.js"
34
+ },
35
+ "./effects": {
36
+ "types": "./_dist/effects.d.ts",
37
+ "default": "./effects.js"
38
+ },
39
+ "./tools": {
40
+ "types": "./_dist/tools.d.ts",
41
+ "default": "./tools.js"
42
+ },
43
+ "./lifecycle": {
44
+ "types": "./_dist/lifecycle.d.ts",
45
+ "default": "./lifecycle.js"
46
+ },
47
+ "./configuration": {
48
+ "types": "./_dist/configuration.d.ts",
49
+ "default": "./configuration.js"
50
+ }
51
+ },
52
+ "license": "Apache-2.0",
53
+ "dependencies": {
54
+ "@yaks/blob": "0.2.36",
55
+ "@yaks/effects": "0.2.36",
56
+ "@yaks/file": "0.2.36",
57
+ "@yaks/fp": "0.2.36",
58
+ "@yaks/graph": "0.2.36",
59
+ "@yaks/machine": "0.2.36",
60
+ "@yaks/process": "0.2.36",
61
+ "@yaks/tools": "0.2.36",
62
+ "@yaks/vocab": "0.2.36"
63
+ },
64
+ "description": "Containers and jails recorded separately from computers, with provisioning intentions and confirmed disposal in the graph.",
65
+ "repository": "https://github.com/yak-sh/yak.git"
66
+ }
package/provider.js ADDED
@@ -0,0 +1,11 @@
1
+ // Sandbox lifecycle is independent of computer connections and session defaults.
2
+ // A resource id is allocated before provisioning and kept while its writable storage is in the trash.
3
+ /** The provider retained this id in trash. A fresh request needs another id. */ export class SandboxReleased extends Error {
4
+ }
5
+ /** Export names are relative and stay inside the sandbox's default directory. */ export let exportPath = (path)=>{
6
+ if (!path || path.startsWith('/') || path.includes('\0') || path.split('/').some((p)=>p == '..' || p == '.')) {
7
+ throw new Error('Sandbox export paths must be relative without . or ..');
8
+ }
9
+ return path;
10
+ };
11
+ //# sourceMappingURL=provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.js","sources":["./provider.ts"],"names":[],"mappings":"AAAA,iFAAiF;AACjF,sGAAsG;AAgBtG,8EAA8E,GAC9E,OAAO,MAAM,wBAAwB;AAAO;AAgB5C,+EAA+E,GAC/E,OAAO,IAAI,aAAa,CAAC;EACvB,IACE,CAAC,QAAQ,KAAK,UAAU,CAAC,QAAQ,KAAK,QAAQ,CAAC,SAC/C,KAAK,KAAK,CAAC,KAAK,IAAI,CAAC,CAAC,IAAM,KAAK,QAAQ,KAAK,MAC9C;IACA,MAAM,IAAI,MAAM;EAClB;EACA,OAAO;AACT,EAAC"}
package/provider.ts ADDED
@@ -0,0 +1,44 @@
1
+ // Sandbox lifecycle is independent of computer connections and session defaults.
2
+ // A resource id is allocated before provisioning and kept while its writable storage is in the trash.
3
+ import type { Workspace } from 'jsr:@yaks/machine@0.2.36'
4
+
5
+ /** A durable sandbox locator; address is never a computer connection address. */
6
+ export type SandboxRef = { id: string; address?: string }
7
+ /** Initial provenance is not a backup of subsequent edits. */
8
+ export type SandboxRequest = SandboxRef & { from?: string; image?: string }
9
+ /** A connection to the existing sandbox filesystem. Placement is held by the
10
+ * sandbox's workspace row, not inferred from this connection's implementation. */
11
+ export type SandboxConnection = {
12
+ workspace: Workspace
13
+ root?: string
14
+ address?: string
15
+ }
16
+ /** One exported file, read inside the sandbox, not through a host symlink. */
17
+ export type SandboxFile = { path: string; bytes: Uint8Array }
18
+ /** The provider retained this id in trash. A fresh request needs another id. */
19
+ export class SandboxReleased extends Error {}
20
+
21
+ /** Configured sandbox lifecycle on an explicit computer (or a backend that
22
+ * cannot disclose placement). connect never creates a replacement resource.
23
+ * dispose retains writable storage in recoverable trash; restore returns it.
24
+ * Neither operation purges storage. Both tolerate repeated calls with the same id. */
25
+ export type SandboxProvider = {
26
+ /** A provider may report a retained, released id with {@link SandboxReleased}. */
27
+ request?: (request: SandboxRequest) => Promise<SandboxConnection>
28
+ /** A provider may report a released id with {@link SandboxReleased}. */
29
+ connect(ref: SandboxRef): Promise<SandboxConnection>
30
+ dispose(ref: SandboxRef): Promise<void>
31
+ restore?(ref: SandboxRef): Promise<void>
32
+ export(ref: SandboxRef, paths: string[]): AsyncIterable<SandboxFile>
33
+ }
34
+
35
+ /** Export names are relative and stay inside the sandbox's default directory. */
36
+ export let exportPath = (path: string): string => {
37
+ if (
38
+ !path || path.startsWith('/') || path.includes('\0') ||
39
+ path.split('/').some((p) => p == '..' || p == '.')
40
+ ) {
41
+ throw new Error('Sandbox export paths must be relative without . or ..')
42
+ }
43
+ return path
44
+ }