-
Notifications
You must be signed in to change notification settings - Fork 941
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e7cafa
commit ef498db
Showing
61 changed files
with
7,600 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,5 @@ coverage | |
*.njsproj | ||
*.sln | ||
*.sw? | ||
|
||
!src/lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.