diff options
author | bartOssh <blenart@eagleeyenetworks.com> | 2020-03-02 17:44:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-02 11:44:46 -0500 |
commit | 4a47ffa5c455be213523cb1a7211a0a454b5fcf8 (patch) | |
tree | 6359cf99d149461ae1435481cc0c919ee357b36e | |
parent | c14cc84a85224870d2188df1a3741bf02eb8c5da (diff) |
seek should return cursor position (#4211)
-rw-r--r-- | cli/js/files.ts | 24 | ||||
-rw-r--r-- | cli/js/files_test.ts | 47 | ||||
-rw-r--r-- | cli/js/io.ts | 18 | ||||
-rw-r--r-- | cli/js/lib.deno.ns.d.ts | 17 | ||||
-rw-r--r-- | cli/js/lib.deno.shared_globals.d.ts | 9 | ||||
-rw-r--r-- | cli/ops/files.rs | 4 |
6 files changed, 79 insertions, 40 deletions
diff --git a/cli/js/files.ts b/cli/js/files.ts index d66870309..dafdd0b32 100644 --- a/cli/js/files.ts +++ b/cli/js/files.ts @@ -216,24 +216,32 @@ export async function write(rid: number, p: Uint8Array): Promise<number> { /** Synchronously seek a file ID to the given offset under mode given by `whence`. * + * Returns the number of cursor position. + * * const file = Deno.openSync("/foo/bar.txt"); - * Deno.seekSync(file.rid, 0, 0); + * const position = Deno.seekSync(file.rid, 0, 0); */ -export function seekSync(rid: number, offset: number, whence: SeekMode): void { - sendSyncJson("op_seek", { rid, offset, whence }); +export function seekSync( + rid: number, + offset: number, + whence: SeekMode +): number { + return sendSyncJson("op_seek", { rid, offset, whence }); } /** Seek a file ID to the given offset under mode given by `whence`. * + * Resolves with the number of cursor position. + * * const file = await Deno.open("/foo/bar.txt"); - * await Deno.seek(file.rid, 0, 0); + * const position = await Deno.seek(file.rid, 0, 0); */ export async function seek( rid: number, offset: number, whence: SeekMode -): Promise<void> { - await sendAsyncJson("op_seek", { rid, offset, whence }); +): Promise<number> { + return await sendAsyncJson("op_seek", { rid, offset, whence }); } /** Close the given resource ID. */ @@ -269,11 +277,11 @@ export class File return readSync(this.rid, p); } - seek(offset: number, whence: SeekMode): Promise<void> { + seek(offset: number, whence: SeekMode): Promise<number> { return seek(this.rid, offset, whence); } - seekSync(offset: number, whence: SeekMode): void { + seekSync(offset: number, whence: SeekMode): number { return seekSync(this.rid, offset, whence); } diff --git a/cli/js/files_test.ts b/cli/js/files_test.ts index 7e3479798..81c379020 100644 --- a/cli/js/files_test.ts +++ b/cli/js/files_test.ts @@ -280,6 +280,7 @@ testPerm( const data = encoder.encode("Hello world!\n"); const file = await Deno.open(filename, "w+"); + const seekPosition = 0; // assert file was created let fileInfo = Deno.statSync(filename); assert(fileInfo.isFile()); @@ -290,7 +291,12 @@ testPerm( assertEquals(fileInfo.len, 13); const buf = new Uint8Array(20); - await file.seek(0, Deno.SeekMode.SEEK_START); + // seeking from beginning of a file + const cursorPosition = await file.seek( + seekPosition, + Deno.SeekMode.SEEK_START + ); + assertEquals(seekPosition, cursorPosition); const result = await file.read(buf); assertEquals(result, 13); file.close(); @@ -302,10 +308,16 @@ testPerm( testPerm({ read: true }, async function seekStart(): Promise<void> { const filename = "cli/tests/hello.txt"; const file = await Deno.open(filename); + const seekPosition = 6; // Deliberately move 1 step forward await file.read(new Uint8Array(1)); // "H" // Skipping "Hello " - await file.seek(6, Deno.SeekMode.SEEK_START); + // seeking from beginning of a file plus seekPosition + const cursorPosition = await file.seek( + seekPosition, + Deno.SeekMode.SEEK_START + ); + assertEquals(seekPosition, cursorPosition); const buf = new Uint8Array(6); await file.read(buf); const decoded = new TextDecoder().decode(buf); @@ -316,10 +328,13 @@ testPerm({ read: true }, async function seekStart(): Promise<void> { testPerm({ read: true }, function seekSyncStart(): void { const filename = "cli/tests/hello.txt"; const file = Deno.openSync(filename); + const seekPosition = 6; // Deliberately move 1 step forward file.readSync(new Uint8Array(1)); // "H" // Skipping "Hello " - file.seekSync(6, Deno.SeekMode.SEEK_START); + // seeking from beginning of a file plus seekPosition + const cursorPosition = file.seekSync(seekPosition, Deno.SeekMode.SEEK_START); + assertEquals(seekPosition, cursorPosition); const buf = new Uint8Array(6); file.readSync(buf); const decoded = new TextDecoder().decode(buf); @@ -333,7 +348,13 @@ testPerm({ read: true }, async function seekCurrent(): Promise<void> { // Deliberately move 1 step forward await file.read(new Uint8Array(1)); // "H" // Skipping "ello " - await file.seek(5, Deno.SeekMode.SEEK_CURRENT); + const seekPosition = 5; + // seekPosition is relative to current cursor position after read + const cursorPosition = await file.seek( + seekPosition, + Deno.SeekMode.SEEK_CURRENT + ); + assertEquals(seekPosition + 1, cursorPosition); const buf = new Uint8Array(6); await file.read(buf); const decoded = new TextDecoder().decode(buf); @@ -347,7 +368,13 @@ testPerm({ read: true }, function seekSyncCurrent(): void { // Deliberately move 1 step forward file.readSync(new Uint8Array(1)); // "H" // Skipping "ello " - file.seekSync(5, Deno.SeekMode.SEEK_CURRENT); + const seekPosition = 5; + // seekPosition is relative to current cursor position after read + const cursorPosition = file.seekSync( + seekPosition, + Deno.SeekMode.SEEK_CURRENT + ); + assertEquals(seekPosition + 1, cursorPosition); const buf = new Uint8Array(6); file.readSync(buf); const decoded = new TextDecoder().decode(buf); @@ -358,7 +385,10 @@ testPerm({ read: true }, function seekSyncCurrent(): void { testPerm({ read: true }, async function seekEnd(): Promise<void> { const filename = "cli/tests/hello.txt"; const file = await Deno.open(filename); - await file.seek(-6, Deno.SeekMode.SEEK_END); + const seekPosition = -6; + // seek from end of file that has 12 chars, 12 - 6 = 6 + const cursorPosition = await file.seek(seekPosition, Deno.SeekMode.SEEK_END); + assertEquals(6, cursorPosition); const buf = new Uint8Array(6); await file.read(buf); const decoded = new TextDecoder().decode(buf); @@ -369,7 +399,10 @@ testPerm({ read: true }, async function seekEnd(): Promise<void> { testPerm({ read: true }, function seekSyncEnd(): void { const filename = "cli/tests/hello.txt"; const file = Deno.openSync(filename); - file.seekSync(-6, Deno.SeekMode.SEEK_END); + const seekPosition = -6; + // seek from end of file that has 12 chars, 12 - 6 = 6 + const cursorPosition = file.seekSync(seekPosition, Deno.SeekMode.SEEK_END); + assertEquals(6, cursorPosition); const buf = new Uint8Array(6); file.readSync(buf); const decoded = new TextDecoder().decode(buf); diff --git a/cli/js/io.ts b/cli/js/io.ts index 151c69f22..e7311d0f4 100644 --- a/cli/js/io.ts +++ b/cli/js/io.ts @@ -105,23 +105,15 @@ export interface Seeker { * and `SEEK_END` means relative to the end. * * Seeking to an offset before the start of the file is an error. Seeking to - * any positive offset is legal, but the behavior of subsequent I/O - * operations on the underlying object is implementation-dependent. + * any positive offset is legal, but the behavior of subsequent I/O operations + * on the underlying object is implementation-dependent. It returns the cursor + * position. */ - seek(offset: number, whence: SeekMode): Promise<void>; + seek(offset: number, whence: SeekMode): Promise<number>; } export interface SyncSeeker { - /** Seek sets the offset for the next `readSync()` or `writeSync()` to - * offset, interpreted according to `whence`: `SEEK_START` means relative - * to the start of the file, `SEEK_CURRENT` means relative to the current - * offset, and `SEEK_END` means relative to the end. - * - * Seeking to an offset before the start of the file is an error. Seeking to - * any positive offset is legal, but the behavior of subsequent I/O - * operations on the underlying object is implementation-dependent. - */ - seekSync(offset: number, whence: SeekMode): void; + seekSync(offset: number, whence: SeekMode): number; } // https://golang.org/pkg/io/#ReadCloser diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index c54876008..50aab6a35 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -383,8 +383,9 @@ declare namespace Deno { * Seeking to an offset before the start of the file is an error. Seeking to * any positive offset is legal, but the behavior of subsequent I/O * operations on the underlying object is implementation-dependent. + * It returns the number of cursor position. */ - seek(offset: number, whence: SeekMode): Promise<void>; + seek(offset: number, whence: SeekMode): Promise<number>; } export interface SyncSeeker { @@ -397,7 +398,7 @@ declare namespace Deno { * any positive offset is legal, but the behavior of subsequent I/O * operations on the underlying object is implementation-dependent. */ - seekSync(offset: number, whence: SeekMode): void; + seekSync(offset: number, whence: SeekMode): number; } export interface ReadCloser extends Reader, Closer {} @@ -525,7 +526,11 @@ declare namespace Deno { * const file = Deno.openSync("/foo/bar.txt"); * Deno.seekSync(file.rid, 0, 0); */ - export function seekSync(rid: number, offset: number, whence: SeekMode): void; + export function seekSync( + rid: number, + offset: number, + whence: SeekMode + ): number; /** Seek a file ID to the given offset under mode given by `whence`. * @@ -536,7 +541,7 @@ declare namespace Deno { rid: number, offset: number, whence: SeekMode - ): Promise<void>; + ): Promise<number>; /** Close the given resource ID. */ export function close(rid: number): void; @@ -557,8 +562,8 @@ declare namespace Deno { writeSync(p: Uint8Array): number; read(p: Uint8Array): Promise<number | EOF>; readSync(p: Uint8Array): number | EOF; - seek(offset: number, whence: SeekMode): Promise<void>; - seekSync(offset: number, whence: SeekMode): void; + seek(offset: number, whence: SeekMode): Promise<number>; + seekSync(offset: number, whence: SeekMode): number; close(): void; } diff --git a/cli/js/lib.deno.shared_globals.d.ts b/cli/js/lib.deno.shared_globals.d.ts index bec9469c5..fef155f3d 100644 --- a/cli/js/lib.deno.shared_globals.d.ts +++ b/cli/js/lib.deno.shared_globals.d.ts @@ -1191,13 +1191,14 @@ declare namespace __io { * relative to the start of the file and an error, if any. * * Seeking to an offset before the start of the file is an error. Seeking to - * any positive offset is legal, but the behavior of subsequent I/O - * operations on the underlying object is implementation-dependent. + * any positive offset is legal, but the behavior of subsequent I/O operations + * on the underlying object is implementation-dependent. + * It returns the cursor position. */ - seek(offset: number, whence: SeekMode): Promise<void>; + seek(offset: number, whence: SeekMode): Promise<number>; } export interface SyncSeeker { - seekSync(offset: number, whence: SeekMode): void; + seekSync(offset: number, whence: SeekMode): number; } export interface ReadCloser extends Reader, Closer {} export interface WriteCloser extends Writer, Closer {} diff --git a/cli/ops/files.rs b/cli/ops/files.rs index 916cbdc69..60fc452a3 100644 --- a/cli/ops/files.rs +++ b/cli/ops/files.rs @@ -204,8 +204,8 @@ fn op_seek( let mut file = futures::executor::block_on(tokio_file.try_clone())?; let fut = async move { - file.seek(seek_from).await?; - Ok(json!({})) + let pos = file.seek(seek_from).await?; + Ok(json!(pos)) }; if args.promise_id.is_none() { |