-
Notifications
You must be signed in to change notification settings - Fork 303
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
b79d069
commit 861aa64
Showing
10 changed files
with
10,378 additions
and
6,664 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
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,18 @@ | ||
import { archivePut, archiveGet } from "../store/archive.js"; | ||
import { getMatchData, getPlayerMatchData } from "../store/queries.js"; | ||
|
||
// Read some match data | ||
const match = await getMatchData(7465883253); | ||
const players = await getPlayerMatchData(7465883253); | ||
const blob = Buffer.from(JSON.stringify({...match, players })); | ||
|
||
// Archive it | ||
await archivePut(match.match_id.toString(), blob); | ||
|
||
// Read it back | ||
const readBack = await archiveGet(match.match_id.toString()); | ||
|
||
console.log(blob.length, readBack.length); | ||
|
||
// Confirm API returns the same data whether we used the archive or not | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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,65 @@ | ||
const config = require("../config"); | ||
const { gzipSync, gunzipSync } = require('zlib'); | ||
const { S3Client, PutObjectCommand, GetObjectCommand } = require("@aws-sdk/client-s3"); | ||
|
||
const client = config.MATCH_ARCHIVE_S3_ENDPOINT ? new S3Client({ | ||
region: 'us-east-1', | ||
credentials: { | ||
accessKeyId: config.MATCH_ARCHIVE_S3_KEY_ID, | ||
secretAccessKey: config.MATCH_ARCHIVE_S3_KEY_SECRET, | ||
}, | ||
endpoint: 'https://' + config.MATCH_ARCHIVE_S3_ENDPOINT, | ||
// any other options are passed to new AWS.S3() | ||
// See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property | ||
}) : null; | ||
|
||
async function stream2buffer(stream) { | ||
return new Promise((resolve, reject) => { | ||
const _buf = []; | ||
stream.on("data", (chunk) => _buf.push(chunk)); | ||
stream.on("end", () => resolve(Buffer.concat(_buf))); | ||
stream.on("error", (err) => reject(err)); | ||
}); | ||
} | ||
|
||
async function archiveGet(key) { | ||
if (!client) { | ||
return; | ||
} | ||
const command = new GetObjectCommand({ | ||
Bucket: config.MATCH_ARCHIVE_S3_BUCKET, | ||
Key: key, | ||
}); | ||
try { | ||
const data = await client.send(command); | ||
if (!data.Body) { | ||
return; | ||
} | ||
const buffer = await stream2buffer(data.Body); | ||
const result = gunzipSync(buffer); | ||
console.log('[ARCHIVE] read %s bytes, decompressed %s bytes', buffer.length, result.length); | ||
return result; | ||
} catch (e) { | ||
return; | ||
} | ||
} | ||
|
||
async function archivePut(key, blob) { | ||
if (!client) { | ||
return; | ||
} | ||
const data = gzipSync(blob); | ||
const command = new PutObjectCommand({ | ||
Bucket: config.MATCH_ARCHIVE_S3_BUCKET, | ||
Key: key, | ||
Body: data, | ||
}); | ||
const result = await client.send(command); | ||
console.log('[ARCHIVE] original %s bytes, archived %s bytes', blob.length, data.length); | ||
return result; | ||
} | ||
|
||
module.exports = { | ||
archiveGet, | ||
archivePut, | ||
}; |
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
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
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
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