Skip to content

Commit

Permalink
Review corrections, tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
dariaterekhova-actionengine committed Dec 6, 2023
1 parent 9e479b1 commit 1c386fb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ export class FileHandleFile implements FileProvider {
/** The FileHandle from which data is provided */
private file: NodeFile;

/** The file length in bytes */
private size: bigint;

/** Create a new FileHandleFile */
constructor(path: string, append: boolean = false) {
this.file = new NodeFile(path, append ? 'a+' : 'r');
this.size = this.file.bigsize;
}

/**
Expand All @@ -27,7 +23,6 @@ export class FileHandleFile implements FileProvider {
*/
async truncate(length: number): Promise<void> {
await this.file.truncate(length);
this.size = (await this.file.stat()).bigsize;
}

/**
Expand All @@ -36,7 +31,6 @@ export class FileHandleFile implements FileProvider {
*/
async append(buffer: Uint8Array): Promise<void> {
await this.file.append(buffer);
this.size = (await this.file.stat()).bigsize;
}

/** Close file */
Expand Down Expand Up @@ -114,6 +108,6 @@ export class FileHandleFile implements FileProvider {
* the length (in bytes) of the data.
*/
get length(): bigint {
return this.size;
return this.file.bigsize;
}
}
4 changes: 2 additions & 2 deletions modules/loader-utils/src/lib/files/node-file-facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ export class NodeFileFacade implements ReadableFile, WritableFile {
throw NOT_IMPLEMENTED;
}

/** Truncates the file descriptor. */
/** Truncates the file descriptor. Only available on NodeFile. */
async truncate(length: number): Promise<void> {
throw NOT_IMPLEMENTED;
}

/** Append data to a file. */
/** Append data to a file. Only available on NodeFile. */
async append(data: Uint8Array): Promise<void> {
throw NOT_IMPLEMENTED;
}
Expand Down
26 changes: 23 additions & 3 deletions modules/polyfills/src/filesystems/node-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,16 @@ export class NodeFile implements ReadableFile, WritableFile {
constructor(path: string, flags: 'r' | 'w' | 'wx' | 'a+', mode?: number) {
path = resolvePath(path);
this.handle = fs.openSync(path, flags, mode);
this.size = 0;
this.bigsize = 0n;
this.updateSize();
this.url = path;
}

updateSize() {
const stats = fs.fstatSync(this.handle, {bigint: true});
this.size = Number(stats.size);
this.bigsize = stats.size;
this.url = path;
}

async close(): Promise<void> {
Expand All @@ -25,13 +31,27 @@ export class NodeFile implements ReadableFile, WritableFile {

async truncate(length: number): Promise<void> {
return new Promise((resolve, reject) => {
fs.ftruncate(this.handle, length, (err) => (err ? reject(err) : resolve()));
fs.ftruncate(this.handle, length, (err) => {
if (err) {
reject(err);
} else {
this.updateSize();
resolve();
}
});
});
}

async append(data: Uint8Array): Promise<void> {
return new Promise((resolve, reject) => {
fs.appendFile(this.handle, data, (err) => (err ? reject(err) : resolve()));
fs.appendFile(this.handle, data, (err) => {
if (err) {
reject(err);
} else {
this.updateSize();
resolve();
}
});
});
}

Expand Down
29 changes: 25 additions & 4 deletions modules/polyfills/test/filesystems/node-file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,37 @@ import test from 'tape-promise/tape';
import {isBrowser} from '@loaders.gl/core';
import {NodeFile} from '@loaders.gl/loader-utils';

const SLPK_URL = '@loaders.gl/i3s/test/data/DA12_subset.slpk';
const SLPK_URL = 'modules/i3s/test/data/DA12_subset.slpk';
const TEST_OFFSET = 100n;

// TODO v4.0 restore this test
test.skip('NodeFile#open and read', async (t) => {
const getSize = async (provider: NodeFile): Promise<bigint> => {
const stat = await provider.stat();
return stat.bigsize;
};

test('NodeFile#open and read', async (t) => {
if (!isBrowser) {
const provider = new NodeFile(SLPK_URL);
const arrayBuffer = await provider.read(4, 1);

const reference = new Buffer(new Uint8Array([0]));
const reference = Buffer.from(new Uint8Array([0x2d]));
t.equals(reference.compare(Buffer.from(arrayBuffer)), 0);
}
t.end();
});

test('NodeFile#truncate and append', async (t) => {
if (!isBrowser) {
const provider = new NodeFile(SLPK_URL, 'a+');
const initialSize = await getSize(provider);

const ending = await provider.read(TEST_OFFSET, Number(initialSize - TEST_OFFSET));

await provider.truncate(Number(TEST_OFFSET));
t.equals(await getSize(provider), TEST_OFFSET);

await provider.append(new Uint8Array(ending));
t.equals(await getSize(provider), initialSize);
}
t.end();
});

0 comments on commit 1c386fb

Please sign in to comment.