Skip to content

Commit

Permalink
Merge pull request #101 from DIG-Network/release/v0.0.1-alpha.111
Browse files Browse the repository at this point in the history
Release/v0.0.1 alpha.111
  • Loading branch information
MichaelTaylor3D authored Oct 2, 2024
2 parents 91778aa + b8e5dfe commit 6ff6b91
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.0.1-alpha.111](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.110...v0.0.1-alpha.111) (2024-10-02)


### Features

* add get key chunk ([13f79bc](https://github.com/DIG-Network/dig-chia-sdk/commit/13f79bc09adaee3631f38ae53febfee1ceadeeda))

### [0.0.1-alpha.110](https://github.com/DIG-Network/dig-chia-sdk/compare/v0.0.1-alpha.109...v0.0.1-alpha.110) (2024-10-02)


Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dignetwork/dig-sdk",
"version": "0.0.1-alpha.110",
"version": "0.0.1-alpha.111",
"description": "",
"type": "commonjs",
"main": "./dist/index.js",
Expand Down
119 changes: 119 additions & 0 deletions src/DigNetwork/ContentServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ export class ContentServer {
return this.fetchWithRetries(url);
}

// New method to get only the first chunk of the content
public async getKeyChunk(
key: string,
rootHash: string,
): Promise<Buffer> {
// Construct the base URL
let url = `https://${formatHost(this.ipAddress)}:${ContentServer.port}/chia.${this.storeId}.${rootHash}/${key}`;
return this.fetchFirstChunk(url);
}

// Method to get the payment address from the peer
public async getPaymentAddress(): Promise<string | null> {
console.log(`Fetching payment address from peer ${this.ipAddress}...`);
Expand Down Expand Up @@ -394,4 +404,113 @@ export class ContentServer {
request.end();
});
}

// New core method to fetch only the first chunk without retries
private async fetchFirstChunk(url: string): Promise<Buffer> {
return new Promise((resolve, reject) => {
const urlObj = new URL(url);
const timeoutDuration = 5000; // 5 seconds

let timeout: NodeJS.Timeout | null = null;

const requestOptions = {
hostname: urlObj.hostname,
port: urlObj.port || ContentServer.port,
path: urlObj.pathname + urlObj.search, // Include query params
method: "GET",
key: fs.readFileSync(ContentServer.keyPath),
cert: fs.readFileSync(ContentServer.certPath),
rejectUnauthorized: false,
};

const request = http.request(requestOptions, (response) => {
// Set timeout for inactivity
timeout = setTimeout(() => {
console.error(
`Request timeout: No data received for ${
timeoutDuration / 1000
} seconds.`
);
request.destroy(); // Use destroy instead of abort
reject(
new Error(
`Request timed out after ${
timeoutDuration / 1000
} seconds of inactivity`
)
);
}, timeoutDuration);

const resetTimeout = () => {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
console.error(
`Request timeout: No data received for ${
timeoutDuration / 1000
} seconds.`
);
request.destroy(); // Use destroy instead of abort
reject(
new Error(
`Request timed out after ${
timeoutDuration / 1000
} seconds of inactivity`
)
);
}, timeoutDuration);
};

if (response.statusCode === 200) {
response.once("data", (chunk) => {
if (timeout) {
clearTimeout(timeout);
}
response.destroy(); // Close the connection after receiving the first chunk
resolve(chunk);
});

response.on("end", () => {
if (timeout) {
clearTimeout(timeout);
}
// In case the response ends before any data is received
reject(new Error("No data received"));
});
} else if (
(response.statusCode === 301 || response.statusCode === 302) &&
response.headers.location
) {
// Handle redirects
const redirectUrl = new URL(response.headers.location, url).toString(); // Resolve relative URLs
if (timeout) {
clearTimeout(timeout);
}
this.fetchFirstChunk(redirectUrl)
.then(resolve)
.catch(reject);
} else {
if (timeout) {
clearTimeout(timeout);
}
reject(
new Error(
`Failed to retrieve data from ${url}. Status code: ${response.statusCode}`
)
);
}
});

request.on("error", (error: NodeJS.ErrnoException) => {
if (timeout) {
clearTimeout(timeout);
}
console.error(`GET ${url}:`, error.message);
reject(error);
});

request.end();
});
}
}
2 changes: 1 addition & 1 deletion src/blockchain/coins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const selectUnspentCoins = async (
const now = Date.now();
const omitCoinIds = omitCoins.map((coin) => getCoinId(coin).toString("hex"));

const validReservedCoins = cachedReservedCoins.filter((coinId) => {
const validReservedCoins = cachedReservedCoins.map((coinId) => {
const reservation = cache.get(coinId);
if (reservation && reservation.expiry > now) {
// Valid reservation, add to omit list
Expand Down

0 comments on commit 6ff6b91

Please sign in to comment.