forked from NiklasPor/nx-remotecache-azure
-
Notifications
You must be signed in to change notification settings - Fork 1
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
6cc760b
commit 0a17556
Showing
6 changed files
with
1,738 additions
and
68 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"env": { | ||
"commonjs": true, | ||
"es2021": true, | ||
"node": true | ||
}, | ||
"extends": [ | ||
"standard-with-typescript", | ||
"prettier" | ||
], | ||
"overrides": [ | ||
], | ||
"parserOptions": { | ||
"ecmaVersion": "latest", | ||
"project": [ | ||
"tsconfig.json" | ||
] | ||
}, | ||
"rules": { | ||
"indent": [ | ||
"warn", | ||
4 | ||
], | ||
"@typescript-eslint/explicit-function-return-type": "off" | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,4 @@ | ||
{} | ||
{ | ||
"singleQuote": true, | ||
"tabWidth": 4 | ||
} |
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,34 @@ | ||
import { type Bucket, type File, Storage } from '@google-cloud/storage'; | ||
|
||
export interface GCPBucketOptions { | ||
googleProject: string; | ||
bucketName: string; | ||
} | ||
|
||
export async function getGCSBucket(options: GCPBucketOptions) { | ||
const bucketURL = options.bucketName; | ||
const storageClient = new Storage({ | ||
projectId: options.googleProject, | ||
retryOptions: { | ||
autoRetry: true, | ||
maxRetries: 4, | ||
}, | ||
}); | ||
const bucket = storageClient.bucket(bucketURL); | ||
|
||
const [bucketExists] = await bucket.exists(); | ||
if (!bucketExists) { | ||
throw Error(`The given Bucket ${bucketURL} does not exist`); | ||
} | ||
|
||
return bucket; | ||
} | ||
|
||
export function constructGCSFile(bucket: Bucket, filename: string) { | ||
return bucket.file(filename); | ||
} | ||
|
||
export async function bucketFileExists(bucketFile: File): Promise<boolean> { | ||
const [exists] = await bucketFile.exists(); | ||
return exists; | ||
} |
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 |
---|---|---|
@@ -1,44 +1,37 @@ | ||
import { createCustomRunner, initEnv } from "nx-remotecache-custom"; | ||
import { Storage } from "@google-cloud/storage"; | ||
|
||
const ENV_GCP_BUCKET_URL = "gs://"; | ||
|
||
const getEnv = (key: string) => process.env[key]; | ||
|
||
async function getGCPFileClient(filename: string, options: AzureBlobRunnerOptions) { | ||
const bucketURL = process.env.GCP_BUCKET_URL; | ||
const storageClient = new Storage(); | ||
const bucket = storageClient.bucket(bucketURL); | ||
const bucketFile = bucket.file(filename); | ||
|
||
|
||
if (!await bucket.exists()) { | ||
throw Error( | ||
`The given Bucket ${bucketURL} does not exist` | ||
); | ||
} | ||
|
||
return bucketFile; | ||
} | ||
|
||
interface AzureBlobRunnerOptions { | ||
connectionString: string; | ||
accountKey: string; | ||
accountName: string; | ||
container: string; | ||
azureUrl: string; | ||
sasUrl: string; | ||
} | ||
|
||
export default createCustomRunner<AzureBlobRunnerOptions>(async (options) => { | ||
initEnv(options); | ||
const GCPFile = async (filename: string) => await getGCPFileClient(filename, options); | ||
|
||
return { | ||
name: "Azure Blob Storage", | ||
fileExists: async (filename) => (await GCPFile(filename)).exists(), | ||
retrieveFile: async (filename) => | ||
(await blob(filename).download()).readableStreamBody!, | ||
storeFile: (filename, stream) => blob(filename).uploadStream(stream) | ||
}; | ||
}); | ||
import { createCustomRunner, initEnv } from 'nx-remotecache-custom'; | ||
import { Readable } from 'stream'; | ||
import { pipeline } from 'stream/promises'; | ||
import type { GCPBucketOptions } from './gcp-specific'; | ||
import { | ||
bucketFileExists, | ||
constructGCSFile, | ||
getGCSBucket, | ||
} from './gcp-specific'; | ||
import type { RemoteCacheImplementation } from 'nx-remotecache-custom/types/remote-cache-implementation'; | ||
|
||
export default createCustomRunner<GCPBucketOptions>( | ||
async (options): Promise<RemoteCacheImplementation> => { | ||
initEnv(options); | ||
const bucket = await getGCSBucket(options); | ||
|
||
return { | ||
name: 'Google Cloud Bucket', | ||
fileExists: async (filename) => { | ||
const bucketFile = constructGCSFile(bucket, filename); | ||
return await bucketFileExists(bucketFile); | ||
}, | ||
retrieveFile: async (filename) => { | ||
const bucketFile = constructGCSFile(bucket, filename); | ||
const downloadedFile = bucketFile.download(); | ||
return Readable.from(await downloadedFile); | ||
}, | ||
storeFile: async (filename, stream) => { | ||
const uploadStream = constructGCSFile( | ||
bucket, | ||
filename | ||
).createWriteStream(); | ||
await pipeline(stream, uploadStream); | ||
}, | ||
}; | ||
} | ||
); |
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
Oops, something went wrong.