Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: - Make it possible to configure timeout on write operations #38

Merged
merged 1 commit into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const sink = new Sink({
This constructor takes the following arguments:

* `storageOptions` - Object - A Google Cloud Storage [storage options object][gcs-storage-options] - Required.
* `writeTimeout` - Number - Timeout, in milliseconds, for write operations to the sink - Default: `30000` - Optional.
* `sinkOptions` - Object - An options object for the sink - See properties below - Optional.
* `sinkOptions.rootPath` - String - Root directory for where to store files in the GCS bucket - Default: `eik` - Optional.
* `sinkOptions.bucket` - String - Name of the bucket to store files in - Default: `eik_files` - Optional.
Expand Down
3 changes: 3 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ const DEFAULT_BUCKET = 'eik_files';

const SinkGCS = class SinkGCS extends Sink {
constructor(storageOptions, {
writeTimeout = 30000,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't 30 seconds seem quite long?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default in the GCS client is 60000 so I kinda made it half. This only affects write so it won't get high traffic.

rootPath = DEFAULT_ROOT_PATH,
bucket = DEFAULT_BUCKET,
} = {}) {
super();
if (typeof storageOptions !== 'object' || storageOptions === null) throw new Error('"storageOptions" argument must be provided');;
this._writeTimeout = writeTimeout;
this._rootPath = rootPath;
this._storage = new Storage(storageOptions);
this._bucket = this._storage.bucket(bucket);
Expand Down Expand Up @@ -51,6 +53,7 @@ const SinkGCS = class SinkGCS extends Sink {
cacheControl: 'public, max-age=31536000',
contentType,
},
timeout: this._writeTimeout,
gzip: true,
});

Expand Down
14 changes: 14 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ test('Sink() - .write() - arguments is illegal', async (t) => {
t.end();
});

test('Sink() - .write() - timeout', async (t) => {
const sink = new Sink(DEFAULT_CONFIG, {
writeTimeout: 40,
});
const dir = slug();
const file = `${dir}/bar/map.json`;

const writeFrom = readFileStream('../fixtures/import-map.json');
const writeTo = await sink.write(file, 'application/json');

t.rejects(pipe(writeFrom, writeTo), /network timeout at/, 'should reject on timeout');
t.end();
});

test('Sink() - .write() - directory traversal prevention', async t => {
const sink = new Sink(DEFAULT_CONFIG);
const dir = slug();
Expand Down