Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

Commit

Permalink
Use node url api (#114)
Browse files Browse the repository at this point in the history
* check if url is supported before using

* let instead of var

* use npm Url

* switch back to private member

* update unit tests

* test for relative path
  • Loading branch information
ericmorgan1 authored and pyu10055 committed May 9, 2018
1 parent f31b41e commit ade1a60
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
},
"dependencies": {
"@types/long": "~3.0.32",
"protobufjs": "~6.8.0"
"protobufjs": "~6.8.0",
"url": "^0.11.0"
}
}
25 changes: 11 additions & 14 deletions src/executor/frozen_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {NamedTensorMap, NamedTensorsMap, tensorflow} from '../data/index';
import {OperationMapper} from '../operations/index';

import {GraphExecutor} from './graph_executor';
import * as Url from 'url';

export class FrozenModel {
private executor: GraphExecutor;
Expand All @@ -42,22 +43,18 @@ export class FrozenModel {
constructor(
private modelUrl: string, private weightManifestUrl: string,
private requestOption?: RequestInit) {
this.getPathPrefix();
this.pathPrefix = this.getPathPrefix();
}

private getPathPrefix() {
const isAbsolute = /^[a-z][a-z0-9+.-]*:/.test(this.weightManifestUrl);
if (isAbsolute) {
const url = new URL(this.weightManifestUrl);
const segments = url.pathname.split('/');
segments.splice(-1);
url.pathname = segments.join('/');
this.pathPrefix = url.toString();
} else {
const segments = this.weightManifestUrl.split('/');
segments.splice(-1);
this.pathPrefix = segments.join('/');
}
/**
* Returns the path prefix for this.weightManifestUrl.
*/
getPathPrefix() {
const url = Url.parse(this.weightManifestUrl);
const segments = url.pathname.split('/');
segments.splice(-1);
url.pathname = segments.join('/');
return Url.format(url) + '/';
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/executor/frozen_model_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ describe('Model', () => {
expect(loaded).toBe(true);
});

describe('getPathPrefix', () => {
it('should set pathPrefix (absolute path)', async () => {
model = new FrozenModel(MODEL_URL, WEIGHT_MANIFEST_URL);
expect(model.getPathPrefix()).toEqual("http://example.org/");
});

it('should set pathPrefix (relative path)', async () => {
model = new FrozenModel(RELATIVE_MODEL_URL, RELATIVE_WEIGHT_MANIFEST_URL);
expect(model.getPathPrefix()).toEqual("/path/");
});
});

describe('eval', () => {
it('should generate the output', async () => {
await model.load();
Expand Down

0 comments on commit ade1a60

Please sign in to comment.