Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: Add TypeScript support #50

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions dist/index.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
interface GCSAdapterOptions {
projectId?: string;
keyFilename?: string;
bucket: string;
bucketPrefix?: string;
directAccess?: boolean;
baseUrl?: string;
}
type GCSAdapterConstructorArgs = [GCSAdapterOptions];
declare class GCSAdapter {
private _bucket;
private _bucketPrefix;
private _directAccess;
private _gcsClient;
private _baseUrl;
constructor(...args: GCSAdapterConstructorArgs);
createFile(filename: string, data: Buffer | string, contentType?: string): Promise<void>;
deleteFile(filename: string): Promise<void>;
getFileData(filename: string): Promise<Buffer>;
getFileLocation(config: {
mount: string;
applicationId: string;
}, filename: string): string;
}

export { GCSAdapter as default };
26 changes: 26 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
interface GCSAdapterOptions {
projectId?: string;
keyFilename?: string;
bucket: string;
bucketPrefix?: string;
directAccess?: boolean;
baseUrl?: string;
}
type GCSAdapterConstructorArgs = [GCSAdapterOptions];
declare class GCSAdapter {
private _bucket;
private _bucketPrefix;
private _directAccess;
private _gcsClient;
private _baseUrl;
constructor(...args: GCSAdapterConstructorArgs);
createFile(filename: string, data: Buffer | string, contentType?: string): Promise<void>;
deleteFile(filename: string): Promise<void>;
getFileData(filename: string): Promise<Buffer>;
getFileLocation(config: {
mount: string;
applicationId: string;
}, filename: string): string;
}

export { GCSAdapter as default };
134 changes: 134 additions & 0 deletions dist/index.js

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

1 change: 1 addition & 0 deletions dist/index.js.map

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

116 changes: 116 additions & 0 deletions dist/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};

// index.ts
import { Storage } from "@google-cloud/storage";
function optionsFromArguments(args) {
const defaultOptions = {
projectId: process.env["GCP_PROJECT_ID"],
keyFilename: process.env["GCP_KEYFILE_PATH"],
bucket: process.env["GCS_BUCKET"],
bucketPrefix: process.env["GCS_BUCKET_PREFIX"] || "",
directAccess: process.env["GCS_DIRECT_ACCESS"] === "true" ? true : false
};
return __spreadValues(__spreadValues({}, defaultOptions), args[0]);
}
var GCSAdapter = class {
constructor(...args) {
let options = optionsFromArguments(args);
this._bucket = options.bucket;
this._bucketPrefix = options.bucketPrefix || "";
this._directAccess = options.directAccess || false;
this._baseUrl = options.baseUrl || "https://storage.googleapis.com/";
this._gcsClient = new Storage(options);
}
createFile(filename, data, contentType) {
return __async(this, null, function* () {
const params = {
metadata: {
contentType: contentType || "application/octet-stream"
}
};
const file = this._gcsClient.bucket(this._bucket).file(this._bucketPrefix + filename);
yield new Promise((resolve, reject) => {
const uploadStream = file.createWriteStream(params);
uploadStream.on("error", reject);
uploadStream.on("finish", resolve);
uploadStream.end(data);
});
if (this._directAccess) {
yield file.makePublic().catch((err) => {
throw new Error(`Error making file public: ${err}`);
});
}
});
}
deleteFile(filename) {
return __async(this, null, function* () {
try {
const file = this._gcsClient.bucket(this._bucket).file(this._bucketPrefix + filename);
yield file.delete();
} catch (err) {
throw new Error(`Error deleting file ${filename}: ${err}`);
}
});
}
getFileData(filename) {
return __async(this, null, function* () {
try {
const file = this._gcsClient.bucket(this._bucket).file(this._bucketPrefix + filename);
const [exists] = yield file.exists();
if (!exists) {
throw new Error(`File ${filename} does not exist.`);
}
const [data] = yield file.download();
return data;
} catch (err) {
throw new Error(`Error downloading file ${filename}: ${err}`);
}
});
}
getFileLocation(config, filename) {
if (this._directAccess) {
return `${this._baseUrl}${this._bucket}/${this._bucketPrefix + filename}`;
}
return `${config.mount}/files/${config.applicationId}/${encodeURIComponent(filename)}`;
}
};
var parse_gcs_default = GCSAdapter;
export {
parse_gcs_default as default
};
//# sourceMappingURL=index.mjs.map
Loading
Loading