1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

seek should return cursor position (#4211)

This commit is contained in:
bartOssh 2020-03-02 17:44:46 +01:00 committed by GitHub
parent c14cc84a85
commit 4a47ffa5c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 79 additions and 40 deletions

View file

@ -215,25 +215,33 @@ 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);
}

View file

@ -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);

View file

@ -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

View file

@ -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;
}

View file

@ -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 {}

View file

@ -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() {