Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
refactor: use symbol for ready property
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Nov 29, 2017
1 parent 4442200 commit 394c42d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 29 deletions.
11 changes: 6 additions & 5 deletions lib/sink.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class ReadStream extends stream.PassThrough {
}

const getGcsFile = Symbol('getGcsFile');
const ready = Symbol('ready');

function getPrefixes(directoryName) {
let prefix;
Expand Down Expand Up @@ -145,7 +146,7 @@ module.exports = class SinkGCS extends EventEmitter {
const bucket = this.gcs.bucket(bucketName);

// `getMetadata` fails if the bucket does not exist
this._ready = bucket
this[ready] = bucket
.getMetadata()
.then(() => {
this.bucket = bucket;
Expand Down Expand Up @@ -176,7 +177,7 @@ module.exports = class SinkGCS extends EventEmitter {
}

async get(fileName) {
await this._ready;
await this[ready];
const data = await this[getGcsFile](fileName).download();
return data.toString();
}
Expand All @@ -201,7 +202,7 @@ module.exports = class SinkGCS extends EventEmitter {
}"`
);

await this._ready;
await this[ready];
await this[getGcsFile](fileName).save(fileContent, {
metadata: {
contentType,
Expand All @@ -211,13 +212,13 @@ module.exports = class SinkGCS extends EventEmitter {

async has(fileName) {
assert(fileName, '"fileName" is missing');
await this._ready;
await this[ready];
const result = await this[getGcsFile](fileName).exists();
return !!(result && result[0]);
}

async dir(directoryName) {
await this._ready;
await this[ready];
try {
const { prefix, filterPrefix } = getPrefixes(directoryName);
const fileRequestResult = await this.bucket.getFiles({
Expand Down
24 changes: 0 additions & 24 deletions test/sink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ test('.writer() - happy path', async done => {
expect.assertions(2);
const sink = getValidSink();

await sink._ready;

const gcsFakeWritestream = new stream.Writable({
_data: false,
write(chunk, encoding, next) {
Expand Down Expand Up @@ -99,8 +97,6 @@ test('.writer() - happy path', async done => {
test('.get() - should resolve fileContent when file exist', async () => {
const sink = getValidSink();

await sink._ready;

const content = 'some-file-content';
sink.gcs._setState({
download: {
Expand All @@ -115,8 +111,6 @@ test('.get() - should resolve fileContent when file exist', async () => {
test('.set() - should return no value/undefined if success', async () => {
const sink = getValidSink();

await sink._ready;

sink.gcs._setState({ save: '' });

const result = await sink.set('some-file.json', 'file-content');
Expand All @@ -137,8 +131,6 @@ test('.set() - should error if file extension cannot be resolved to a mime type'
test('.has() - should return true if file exists', async () => {
const sink = getValidSink();

await sink._ready;

sink.gcs._setState({ exists: [true] });

const result = await sink.has('some-file');
Expand All @@ -149,8 +141,6 @@ test('.has() - should return true if file exists', async () => {
test('.has() - should return false if missing', async () => {
const sink = getValidSink();

await sink._ready;

sink.gcs._setState({ exists: [] });

const result = await sink.has('some-file');
Expand All @@ -168,8 +158,6 @@ test('dir() - should error when invalid response from getFiles', async () => {
expect.assertions(1);
const sink = getValidSink();

await sink._ready;

sink.gcs._setState({ getFiles: { '/': [] } });

try {
Expand All @@ -183,8 +171,6 @@ test('dir() - should error when no files', async () => {
expect.assertions(1);
const sink = getValidSink();

await sink._ready;

sink.gcs._setState({ getFiles: { 'some-dir/': [[]] } });

try {
Expand All @@ -198,8 +184,6 @@ test('dir() - should error when no matching files', async () => {
expect.assertions(1);
const sink = getValidSink();

await sink._ready;

sink.gcs._setState({
getFiles: {
'folder/': [
Expand All @@ -220,8 +204,6 @@ test('dir() - should output 1 file', async () => {
expect.assertions(1);
const sink = getValidSink();

await sink._ready;

const fileName = 'some-path/some-file-name.json';
sink.gcs._setState({
download: { [fileName]: 'file-content' },
Expand All @@ -238,8 +220,6 @@ test('dir() - should output 1 file with extra slash', async () => {
expect.assertions(1);
const sink = getValidSink();

await sink._ready;

const fileName = 'some-path/some-file-name.json';
sink.gcs._setState({
download: { [fileName]: 'file-content' },
Expand All @@ -256,8 +236,6 @@ test('dir() - should output 3 file', async () => {
expect.assertions(1);
const sink = getValidSink();

await sink._ready;

const fileName1 = 'some-path/some-file-name-1';
const fileName2 = 'some-path/some-file-name-2';
const fileName3 = 'some-path/some-file-name-3';
Expand Down Expand Up @@ -286,8 +264,6 @@ test('dir() - should output 3 files with extensions', async () => {
expect.assertions(1);
const sink = getValidSink();

await sink._ready;

const fileName1 = 'some-path/some-file-name-4.json';
const fileName2 = 'some-path/some-file-name-5.css';
const fileName3 = 'some-path/some-file-name-6.js';
Expand Down

0 comments on commit 394c42d

Please sign in to comment.