@phpsandbox/sdk 0.1.2 → 0.1.4

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.
@@ -68,6 +68,9 @@ export interface FileListOptions {
68
68
  export interface FileTailOptions {
69
69
  lines?: number;
70
70
  }
71
+ export interface FileFollowOptions extends FileTailOptions {
72
+ signal?: AbortSignal;
73
+ }
71
74
  export interface FileLineRange {
72
75
  start: number;
73
76
  end: number;
@@ -403,6 +406,14 @@ export interface FilesystemActions {
403
406
  'fs.cancelRead': Action<{
404
407
  id: string;
405
408
  }, boolean>;
409
+ 'fs.follow': Action<{
410
+ id: string;
411
+ path: string;
412
+ lines: number;
413
+ }, boolean>;
414
+ 'fs.cancelFollow': Action<{
415
+ id: string;
416
+ }, boolean>;
406
417
  'fs.writeStream': Action<{
407
418
  id: string;
408
419
  path: string;
@@ -471,6 +482,10 @@ interface FilesystemEventData {
471
482
  id: string;
472
483
  data: Uint8Array;
473
484
  };
485
+ 'fs.follow': {
486
+ id: string;
487
+ data: Uint8Array;
488
+ };
474
489
  }
475
490
  type PrefixKey<K extends keyof FilesystemEventData> = `${K}.${string}`;
476
491
  export type FilesystemEvents = FilesystemEventData & {
@@ -505,6 +520,7 @@ export declare class Filesystem {
505
520
  readText(path: string): Promise<string>;
506
521
  readLines(path: string, range: FileLineRange): Promise<FileLineRangeResult>;
507
522
  readStream(path: string): ReadableStream<Uint8Array>;
523
+ follow(path: string, options?: FileFollowOptions): ReadableStream<Uint8Array>;
508
524
  find(query: string, options?: Partial<FileSearchOptions>): Promise<FileResult[]>;
509
525
  search(query: TextSearchQuery, options?: Partial<TextSearchOptions>, onMatch?: (result: TextSearchResult | false) => boolean | Promise<boolean> | void | Promise<void>): Promise<[boolean, TextSearchMatch[]]>;
510
526
  tail(path: string, options?: FileTailOptions): Promise<string>;
@@ -1,3 +1,4 @@
1
+ import { UnsupportedRuntimeTransportError } from './errors/index.js';
1
2
  import { ErrorEvent } from './types.js';
2
3
  import { nanoid } from 'nanoid';
3
4
  export var FileType;
@@ -204,6 +205,77 @@ export class Filesystem {
204
205
  },
205
206
  });
206
207
  }
208
+ follow(path, options = {}) {
209
+ if (this.okra.runtimeTransport === 'rest') {
210
+ return new ReadableStream({
211
+ start(controller) {
212
+ controller.error(new UnsupportedRuntimeTransportError('Following files requires the realtime transport.'));
213
+ },
214
+ });
215
+ }
216
+ const id = nanoid();
217
+ let disposable = null;
218
+ let abort = null;
219
+ let started = false;
220
+ let closed = false;
221
+ const stop = async () => {
222
+ if (closed) {
223
+ return;
224
+ }
225
+ closed = true;
226
+ disposable?.dispose();
227
+ disposable = null;
228
+ if (abort && options.signal) {
229
+ options.signal.removeEventListener('abort', abort);
230
+ }
231
+ abort = null;
232
+ if (started) {
233
+ await this.okra.invoke('fs.cancelFollow', { id }).catch(() => false);
234
+ }
235
+ };
236
+ return new ReadableStream({
237
+ start: (controller) => {
238
+ disposable = this.okra.listen(`fs.follow.${id}`, (event) => {
239
+ if (!closed) {
240
+ controller.enqueue(event.data);
241
+ }
242
+ });
243
+ if (options.signal) {
244
+ abort = () => {
245
+ void stop().finally(() => controller.error(options.signal?.reason ?? new Error('Aborted')));
246
+ };
247
+ if (options.signal.aborted) {
248
+ abort();
249
+ return;
250
+ }
251
+ options.signal.addEventListener('abort', abort, { once: true });
252
+ }
253
+ started = true;
254
+ void this.okra.invoke('fs.follow', { id, path, lines: options.lines ?? 10 }).then(() => {
255
+ if (!closed) {
256
+ closed = true;
257
+ disposable?.dispose();
258
+ if (abort && options.signal) {
259
+ options.signal.removeEventListener('abort', abort);
260
+ }
261
+ abort = null;
262
+ controller.close();
263
+ }
264
+ }).catch((error) => {
265
+ if (!closed) {
266
+ closed = true;
267
+ disposable?.dispose();
268
+ if (abort && options.signal) {
269
+ options.signal.removeEventListener('abort', abort);
270
+ }
271
+ abort = null;
272
+ controller.error(error);
273
+ }
274
+ });
275
+ },
276
+ cancel: stop,
277
+ });
278
+ }
207
279
  find(query, options = {
208
280
  useIgnoreFiles: false,
209
281
  followSymlinks: false,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phpsandbox/sdk",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "description": "A comprehensive TypeScript SDK for interacting with cloud-based PHP development environments",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",