Skip to content

Commit

Permalink
Avoid null as return type
Browse files Browse the repository at this point in the history
  • Loading branch information
javagl committed Jan 29, 2025
1 parent 85f3ab5 commit 6437281
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 52 deletions.
8 changes: 4 additions & 4 deletions specs/base/contentTypes/LazyContentDataSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ function createTestResourceResolver(): ResourceResolver {
resolveUri(uri: string) {
return uri;
},
async resolveData(uri: string): Promise<Buffer | null> {
return null;
async resolveData(uri: string): Promise<Buffer | undefined> {
return undefined;
},
async resolveDataPartial(
uri: string,
maxBytes: number
): Promise<Buffer | null> {
return null;
): Promise<Buffer | undefined> {
return undefined;
},
derive(uri: string): ResourceResolver {
return this;
Expand Down
12 changes: 6 additions & 6 deletions src/base/contentTypes/BufferedContentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class BufferedContentData implements ContentData {
* @returns The ContentData
*/
static create(uri: string): ContentData {
let data: Buffer | null = null;
let data: Buffer | undefined = undefined;
try {
data = fs.readFileSync(uri);
} catch (error) {
Expand Down Expand Up @@ -61,10 +61,10 @@ export class BufferedContentData implements ContentData {
private readonly _magic: Buffer;

/**
* The content data, or `null` if the data could not
* The content data, or `undefined` if the data could not
* be resolved.
*/
private readonly _data: Buffer | null;
private readonly _data: Buffer | undefined;

/**
* The object that was parsed from the content, assuming
Expand All @@ -86,7 +86,7 @@ export class BufferedContentData implements ContentData {
* @param uri - The URI of the content data
* @param data - The actual content data buffer
*/
constructor(uri: string, data: Buffer | null) {
constructor(uri: string, data: Buffer | undefined) {
this._uri = uri;
this._extension = path.extname(uri).toLowerCase();
if (data) {
Expand All @@ -112,7 +112,7 @@ export class BufferedContentData implements ContentData {

/** {@inheritDoc ContentData.exists} */
async exists(): Promise<boolean> {
return this._data !== null;
return this._data !== undefined;
}

/** {@inheritDoc ContentData.magic} */
Expand All @@ -121,7 +121,7 @@ export class BufferedContentData implements ContentData {
}

/** {@inheritDoc ContentData.data} */
async getData(): Promise<Buffer | null> {
async getData(): Promise<Buffer | undefined> {
return this._data;
}

Expand Down
4 changes: 2 additions & 2 deletions src/base/contentTypes/ContentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ export interface ContentData {
/**
* Returns the actual content data that was read from the URI.
*
* @returns The promise to the data, or to `null` when the
* @returns The promise to the data, or to `undefined` when the
* data could not be obtained.
*/
getData(): Promise<Buffer | null>;
getData(): Promise<Buffer | undefined>;

/**
* Returns the object that was parsed from the content data,
Expand Down
12 changes: 6 additions & 6 deletions src/base/contentTypes/LazyContentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export class LazyContentData implements ContentData {
private _magic: Buffer | undefined;

/**
* The content data, or `null` if the data could not
* The content data, or `undefined` if the data could not
* be resolved.
*/
private _data: Buffer | null;
private _data: Buffer | undefined;

/**
* Whether the `_data` was already requested
Expand Down Expand Up @@ -87,7 +87,7 @@ export class LazyContentData implements ContentData {
this._resourceResolver = resourceResolver;
this._extension = path.extname(uri).toLowerCase();
this._magic = undefined;
this._data = null;
this._data = undefined;
this._dataWasRequested = false;
this._parsedObject = undefined;
this._parsedObjectWasRequested = false;
Expand All @@ -112,7 +112,7 @@ export class LazyContentData implements ContentData {
this._uri,
1
);
this._exists = partialData !== null;
this._exists = partialData !== undefined;
return this._exists;
}

Expand All @@ -137,13 +137,13 @@ export class LazyContentData implements ContentData {
}

/** {@inheritDoc ContentData.getData} */
async getData(): Promise<Buffer | null> {
async getData(): Promise<Buffer | undefined> {
if (this._dataWasRequested) {
return this._data;
}
this._data = await this._resourceResolver.resolveData(this._uri);
this._dataWasRequested = true;
this._exists = this._data !== null;
this._exists = this._data !== undefined;
return this._data;
}

Expand Down
12 changes: 6 additions & 6 deletions src/base/io/FileResourceResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ export class FileResourceResolver implements ResourceResolver {
async resolveDataPartial(
uri: string,
maxBytes: number
): Promise<Buffer | null> {
): Promise<Buffer | undefined> {
if (Uris.isDataUri(uri)) {
const data = Buffer.from(uri.split(",")[1], "base64");
return data;
}
if (Uris.isAbsoluteUri(uri)) {
return null;
return undefined;
}
const resolved = this.resolveUri(uri);
try {
Expand All @@ -44,22 +44,22 @@ export class FileResourceResolver implements ResourceResolver {
fs.closeSync(fd);
return buffer;
} catch (error) {
return null;
return undefined;
}
}

/** {@inheritDoc ResourceResolver.resolveData} */
async resolveData(uri: string): Promise<Buffer | null> {
async resolveData(uri: string): Promise<Buffer | undefined> {
if (Uris.isDataUri(uri)) {
const data = Buffer.from(uri.split(",")[1], "base64");
return data;
}
if (Uris.isAbsoluteUri(uri)) {
return null;
return undefined;
}
const resolved = this.resolveUri(uri);
if (!fs.existsSync(resolved)) {
return null;
return undefined;
}
const data = fs.readFileSync(resolved);
// See https://github.com/nodejs/node/issues/35351
Expand Down
13 changes: 8 additions & 5 deletions src/base/io/ResourceResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ export interface ResourceResolver {
*
* @param uri - The URI
* @returns A promise that resolves with the buffer data,
* or with `null` if the resource could not be resolved.
* or with `undefined` if the resource could not be resolved.
*/
resolveData(uri: string): Promise<Buffer | null>;
resolveData(uri: string): Promise<Buffer | undefined>;

/**
* Resolve parts of the data from the given URI.
*
* The given URI may be relative to the base URI for
* which this instance has been created.
*
* If the resource cannot be resolved, then `null` is
* If the resource cannot be resolved, then `undefined` is
* returned.
*
* Otherwise, the returned buffer contains _at least_ the
Expand All @@ -46,9 +46,12 @@ export interface ResourceResolver {
*
* @param uri - The URI
* @returns A promise that resolves with the buffer data,
* or with `null` if the resource could not be resolved.
* or with `undefined` if the resource could not be resolved.
*/
resolveDataPartial(uri: string, maxBytes: number): Promise<Buffer | null>;
resolveDataPartial(
uri: string,
maxBytes: number
): Promise<Buffer | undefined>;

/**
* Derive an instance from this one, with a different base
Expand Down
16 changes: 8 additions & 8 deletions src/base/io/UnzippingResourceResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export class UnzippingResourceResolver implements ResourceResolver {
}

/** {@inheritDoc ResourceResolver.resolveData} */
async resolveData(uri: string): Promise<Buffer | null> {
async resolveData(uri: string): Promise<Buffer | undefined> {
const delegateData = await this._delegate.resolveData(uri);
if (delegateData === null) {
return null;
if (delegateData === undefined) {
return undefined;
}
const isGzipped = Buffers.isGzipped(delegateData);
if (!isGzipped) {
Expand All @@ -40,21 +40,21 @@ export class UnzippingResourceResolver implements ResourceResolver {
async resolveDataPartial(
uri: string,
maxBytes: number
): Promise<Buffer | null> {
): Promise<Buffer | undefined> {
const partialDelegateData = await this._delegate.resolveDataPartial(
uri,
maxBytes
);
if (partialDelegateData === null) {
return null;
if (partialDelegateData === undefined) {
return undefined;
}
const isGzipped = Buffers.isGzipped(partialDelegateData);
if (!isGzipped) {
return partialDelegateData;
}
const fullDelegateData = await this._delegate.resolveData(uri);
if (fullDelegateData === null) {
return null;
if (fullDelegateData === undefined) {
return undefined;
}
const data = zlib.gunzipSync(fullDelegateData);
return data;
Expand Down
4 changes: 2 additions & 2 deletions src/base/spatial/OctreeCoordinates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export class OctreeCoordinates implements TreeCoordinates {
}

/** {@inheritDoc TreeCoordinates.parent} */
parent(): OctreeCoordinates | null {
parent(): OctreeCoordinates | undefined {
if (this._level === 0) {
return null;
return undefined;
}
const pLevel = this._level - 1;
const px = this._x >> 1;
Expand Down
4 changes: 2 additions & 2 deletions src/base/spatial/QuadtreeCoordinates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export class QuadtreeCoordinates implements TreeCoordinates {
}

/** {@inheritDoc TreeCoordinates.parent} */
parent(): QuadtreeCoordinates | null {
parent(): QuadtreeCoordinates | undefined {
if (this._level === 0) {
return null;
return undefined;
}
const pLevel = this._level - 1;
const px = this._x >> 1;
Expand Down
4 changes: 2 additions & 2 deletions src/base/spatial/TreeCoordinates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export interface TreeCoordinates {

/**
* Returns the parent coordinates of these coordinates,
* or `null` if this is the root.
* or `undefined` if this is the root.
*
* @returns The parent coordinates
*/
parent(): TreeCoordinates | null;
parent(): TreeCoordinates | undefined;

/**
* Returns a generator for the child coordinates of these coordinates
Expand Down
7 changes: 4 additions & 3 deletions src/tilesets/tilesetData/TilesetSourceFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ export class TilesetSourceFs implements TilesetSource {
if (!fs.existsSync(fullFileName)) {
return undefined;
}
const data = fs.readFileSync(fullFileName);
if (data === null) {
try {
const data = fs.readFileSync(fullFileName);
return data;
} catch (error) {
return undefined;
}
return data;
}

/** {@inheritDoc TilesetSource.close} */
Expand Down
12 changes: 6 additions & 6 deletions src/tilesets/tilesetData/TilesetSourceResourceResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,34 @@ export class TilesetSourceResourceResolver implements ResourceResolver {
}

/** {@inheritDoc ResourceResolver.resolveData} */
async resolveData(uri: string): Promise<Buffer | null> {
async resolveData(uri: string): Promise<Buffer | undefined> {
return this.resolveDataInternal(uri);
}

/** {@inheritDoc ResourceResolver.resolveDataPartial} */
async resolveDataPartial(
uri: string,
maxBytes: number
): Promise<Buffer | null> {
): Promise<Buffer | undefined> {
const buffer = await this.resolveDataInternal(uri);
if (!buffer) {
return null;
return undefined;
}
return buffer.subarray(0, maxBytes);
}

private async resolveDataInternal(uri: string): Promise<Buffer | null> {
private async resolveDataInternal(uri: string): Promise<Buffer | undefined> {
if (Uris.isDataUri(uri)) {
const data = Buffer.from(uri.split(",")[1], "base64");
return data;
}
if (Uris.isAbsoluteUri(uri)) {
return null;
return undefined;
}
const localUri = this.resolveUri(uri);
const value = await this._tilesetSource.getValue(localUri);
if (!value) {
return null;
return undefined;
}
return value;
}
Expand Down

0 comments on commit 6437281

Please sign in to comment.