Skip to content

Commit

Permalink
Update gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesbvll committed Oct 7, 2024
1 parent 6e7cafa commit ef498db
Show file tree
Hide file tree
Showing 61 changed files with 7,600 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ coverage
*.njsproj
*.sln
*.sw?

!src/lib
38 changes: 38 additions & 0 deletions src/ts/src/lib/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseAddress = parseAddress;
const net_1 = require("net");
const IPV6 = 6;
const IPV4 = 4;
function parseAddress(address) {
try {
const lastColonIndex = address.lastIndexOf(":");
if (lastColonIndex === -1) {
throw new Error("No port was provided.");
}
// Split the address into host and port.
const rawHost = address.slice(0, lastColonIndex);
const rawPort = address.slice(lastColonIndex + 1);
const port = parseInt(rawPort, 10);
if (port > 65535 || port < 1) {
throw new Error("Port number is invalid.");
}
let host = rawHost.replace(/[\[\]]/g, ""); // Remove brackets for IPv6
let version = null;
const ipVersion = (0, net_1.isIP)(host);
if (ipVersion === IPV6) {
version = true;
}
else if (ipVersion === IPV4) {
version = false;
}
return {
host,
port,
version,
};
}
catch (err) {
return null;
}
}
49 changes: 49 additions & 0 deletions src/ts/src/lib/address.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const address_1 = require("./address");
describe("parseAddress", () => {
test("parses a valid IPv4 address", () => {
const result = (0, address_1.parseAddress)("127.0.0.1:8080");
expect(result).toEqual({
host: "127.0.0.1",
port: 8080,
version: false, // IPv4 address
});
});
test("parses a valid IPv6 address", () => {
const result = (0, address_1.parseAddress)("[::1]:8080");
expect(result).toEqual({
host: "::1",
port: 8080,
version: true, // IPv6 address
});
});
test("returns null for an invalid port number", () => {
const result = (0, address_1.parseAddress)("127.0.0.1:70000"); // Invalid port
expect(result).toBeNull();
});
test("returns null for missing port", () => {
const result = (0, address_1.parseAddress)("127.0.0.1"); // No port provided
expect(result).toBeNull();
});
test("returns null for an invalid address format", () => {
const result = (0, address_1.parseAddress)("notAnAddress");
expect(result).toBeNull();
});
test("parses domain names correctly", () => {
const result = (0, address_1.parseAddress)("example.com:8080");
expect(result).toEqual({
host: "example.com",
port: 8080,
version: null, // Domain names do not have IP versions
});
});
test("parses IPv6 with brackets and returns proper version", () => {
const result = (0, address_1.parseAddress)("[2001:db8::ff00:42:8329]:9090");
expect(result).toEqual({
host: "2001:db8::ff00:42:8329",
port: 9090,
version: true, // IPv6 address
});
});
});
54 changes: 54 additions & 0 deletions src/ts/src/lib/address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { parseAddress } from './address';

describe("parseAddress", () => {
test("parses a valid IPv4 address", () => {
const result = parseAddress("127.0.0.1:8080");
expect(result).toEqual({
host: "127.0.0.1",
port: 8080,
version: false, // IPv4 address
});
});

test("parses a valid IPv6 address", () => {
const result = parseAddress("[::1]:8080");
expect(result).toEqual({
host: "::1",
port: 8080,
version: true, // IPv6 address
});
});

test("returns null for an invalid port number", () => {
const result = parseAddress("127.0.0.1:70000"); // Invalid port
expect(result).toBeNull();
});

test("returns null for missing port", () => {
const result = parseAddress("127.0.0.1"); // No port provided
expect(result).toBeNull();
});

test("returns null for an invalid address format", () => {
const result = parseAddress("notAnAddress");
expect(result).toBeNull();
});

test("parses domain names correctly", () => {
const result = parseAddress("example.com:8080");
expect(result).toEqual({
host: "example.com",
port: 8080,
version: null, // Domain names do not have IP versions
});
});

test("parses IPv6 with brackets and returns proper version", () => {
const result = parseAddress("[2001:db8::ff00:42:8329]:9090");
expect(result).toEqual({
host: "2001:db8::ff00:42:8329",
port: 9090,
version: true, // IPv6 address
});
});
});
48 changes: 48 additions & 0 deletions src/ts/src/lib/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { isIP } from "net";

const IPV6 = 6;
const IPV4 = 4;

interface ParsedAddress {
host: string;
port: number;
version: boolean | null;
}

export function parseAddress(address: string): ParsedAddress | null {
try {
const lastColonIndex = address.lastIndexOf(":");

if (lastColonIndex === -1) {
throw new Error("No port was provided.");
}

// Split the address into host and port.
const rawHost = address.slice(0, lastColonIndex);
const rawPort = address.slice(lastColonIndex + 1);

const port = parseInt(rawPort, 10);

if (port > 65535 || port < 1) {
throw new Error("Port number is invalid.");
}

let host = rawHost.replace(/[\[\]]/g, ""); // Remove brackets for IPv6
let version: boolean | null = null;

const ipVersion = isIP(host);
if (ipVersion === IPV6) {
version = true;
} else if (ipVersion === IPV4) {
version = false;
}

return {
host,
port,
version,
};
} catch (err) {
return null;
}
}
111 changes: 111 additions & 0 deletions src/ts/src/lib/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"use strict";
// Copyright 2024 Flower Labs GmbH. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ==============================================================================
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = void 0;
exports.maybeCallGetProperties = maybeCallGetProperties;
exports.maybeCallGetParameters = maybeCallGetParameters;
exports.maybeCallFit = maybeCallFit;
exports.maybeCallEvaluate = maybeCallEvaluate;
const typing_1 = require("./typing");
class BaseClient {
context;
constructor(context) {
this.context = context;
}
setContext(context) {
this.context = context;
}
getContext() {
return this.context;
}
}
class Client extends BaseClient {
getProperties(_ins) {
return {
status: {
code: typing_1.Code.GET_PROPERTIES_NOT_IMPLEMENTED,
message: "Client does not implement `get_properties`",
},
properties: {},
};
}
}
exports.Client = Client;
function hasGetProperties(client) {
return client.getProperties !== undefined;
}
function hasGetParameters(client) {
return client.getParameters !== undefined;
}
function hasFit(client) {
return client.fit !== undefined;
}
function hasEvaluate(client) {
return client.evaluate !== undefined;
}
function maybeCallGetProperties(client, getPropertiesIns) {
if (!hasGetProperties(client)) {
const status = {
code: typing_1.Code.GET_PROPERTIES_NOT_IMPLEMENTED,
message: "Client does not implement `get_properties`",
};
return { status, properties: {} };
}
return client.getProperties(getPropertiesIns);
}
function maybeCallGetParameters(client, getParametersIns) {
if (!hasGetParameters(client)) {
const status = {
code: typing_1.Code.GET_PARAMETERS_NOT_IMPLEMENTED,
message: "Client does not implement `get_parameters`",
};
return {
status,
parameters: { tensorType: "", tensors: [] },
};
}
return client.getParameters(getParametersIns);
}
function maybeCallFit(client, fitIns) {
if (!hasFit(client)) {
const status = {
code: typing_1.Code.FIT_NOT_IMPLEMENTED,
message: "Client does not implement `fit`",
};
return {
status,
parameters: { tensorType: "", tensors: [] },
numExamples: 0,
metrics: {},
};
}
return client.fit(fitIns);
}
function maybeCallEvaluate(client, evaluateIns) {
if (!hasEvaluate(client)) {
const status = {
code: typing_1.Code.EVALUATE_NOT_IMPLEMENTED,
message: "Client does not implement `evaluate`",
};
return {
status,
loss: 0.0,
numExamples: 0,
metrics: {},
};
}
return client.evaluate(evaluateIns);
}
Loading

0 comments on commit ef498db

Please sign in to comment.