Skip to content

Commit 3956895

Browse files
authored
Merge pull request #192 from opencomponents/update-adapter
upgrade google storage adapter and fix storage-client polling issues
2 parents f164e7f + 714c394 commit 3956895

File tree

4 files changed

+108
-96
lines changed

4 files changed

+108
-96
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
jest.mock('fs-extra', () => {
2+
return {
3+
createReadStream: jest.fn(() => 'this is a stream'),
4+
readFile: jest.fn(cb => cb(null, 'file content!'))
5+
};
6+
});
7+
8+
jest.mock('node-dir', () => {
9+
return {
10+
paths: jest.fn((pathToDir, cb) => {
11+
const sep = require('path').sep;
12+
cb(null, {
13+
files: [
14+
`${pathToDir}${sep}package.json`,
15+
`${pathToDir}${sep}server.js`,
16+
`${pathToDir}${sep}template.js`
17+
]
18+
});
19+
})
20+
};
21+
});
22+
23+
let mockCachedTxt = 0;
24+
let mockCachedJson = 0;
25+
const googleStorage = jest.genMockFromModule('@google-cloud/storage');
26+
27+
const _Storage = class {
28+
constructor() {
29+
this.bucket = jest.fn(bucket => ({
30+
getFiles: () => {
31+
const files =
32+
bucket === 'my-empty-bucket'
33+
? []
34+
: [
35+
[
36+
{
37+
name: 'components/image/1.0.0/app.js'
38+
},
39+
{
40+
name: 'components/image/1.0.0/server.js'
41+
},
42+
{
43+
name: 'components/image/1.0.1/new-server.js'
44+
},
45+
{
46+
name: 'components/image/1.0.1/new-app.js'
47+
}
48+
]
49+
];
50+
return Promise.resolve(files);
51+
},
52+
upload: (filePath, { destination }) => {
53+
if (destination.match('-error')) {
54+
return Promise.reject({
55+
code: 1234,
56+
message: 'an error message'
57+
});
58+
}
59+
return Promise.resolve();
60+
},
61+
file: file => ({
62+
makePublic() {
63+
return Promise.resolve();
64+
},
65+
download() {
66+
mockCachedTxt++;
67+
mockCachedJson++;
68+
const contents = {
69+
'path/test.txt': 'Hello!',
70+
'path/test.json': JSON.stringify({ value: 'Hello!' }),
71+
'path/not-found.txt': { error: { code: 404 } },
72+
'path/not-found.json': { error: { code: 404 } },
73+
'path/not-a-json.json': {
74+
error: { code: '1', msg: 'not an error' }
75+
},
76+
'path/to-mutable.json': JSON.stringify({ value: mockCachedJson }),
77+
'path/to-mutable.txt': mockCachedTxt
78+
};
79+
const content = contents[file];
80+
if (content.error) {
81+
return Promise.reject(content.error);
82+
} else {
83+
return Promise.resolve(content);
84+
}
85+
}
86+
})
87+
}));
88+
}
89+
};
90+
91+
googleStorage.Storage = _Storage;
92+
module.exports = googleStorage;

packages/oc-gs-storage-adapter/__test__/gs.test.js

Lines changed: 2 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -9,95 +9,6 @@ global.Date.UTC = _Date.UTC;
99
global.Date.parse = _Date.parse;
1010
global.Date.now = _Date.now;
1111

12-
jest.mock('fs-extra', () => {
13-
return {
14-
createReadStream: jest.fn(() => 'this is a stream'),
15-
writeFileSync: jest.fn(() => 'write file'),
16-
readFile: jest.fn(cb => cb(null, 'file content!'))
17-
};
18-
});
19-
20-
jest.mock('node-dir', () => {
21-
return {
22-
paths: jest.fn((pathToDir, cb) => {
23-
const sep = require('path').sep;
24-
cb(null, {
25-
files: [
26-
`${pathToDir}${sep}package.json`,
27-
`${pathToDir}${sep}server.js`,
28-
`${pathToDir}${sep}template.js`
29-
]
30-
});
31-
})
32-
};
33-
});
34-
35-
let mockCachedTxt = 0;
36-
let mockCachedJson = 0;
37-
jest.mock('@google-cloud/storage', () =>
38-
jest.fn(() => ({
39-
bucket: bucket => ({
40-
getFiles: () => {
41-
const files =
42-
bucket === 'my-empty-bucket'
43-
? []
44-
: [
45-
[
46-
{
47-
name: 'components/image/1.0.0/app.js'
48-
},
49-
{
50-
name: 'components/image/1.0.0/server.js'
51-
},
52-
{
53-
name: 'components/image/1.0.1/new-server.js'
54-
},
55-
{
56-
name: 'components/image/1.0.1/new-app.js'
57-
}
58-
]
59-
];
60-
return Promise.resolve(files);
61-
},
62-
upload: (filePath, { destination }) => {
63-
if (destination.match('-error')) {
64-
return Promise.reject({
65-
code: 1234,
66-
message: 'an error message'
67-
});
68-
}
69-
return Promise.resolve();
70-
},
71-
file: file => ({
72-
makePublic() {
73-
return Promise.resolve();
74-
},
75-
download() {
76-
mockCachedTxt++;
77-
mockCachedJson++;
78-
const contents = {
79-
'path/test.txt': 'Hello!',
80-
'path/test.json': JSON.stringify({ value: 'Hello!' }),
81-
'path/not-found.txt': { error: { code: 404 } },
82-
'path/not-found.json': { error: { code: 404 } },
83-
'path/not-a-json.json': {
84-
error: { code: '1', msg: 'not an error' }
85-
},
86-
'path/to-mutable.json': JSON.stringify({ value: mockCachedJson }),
87-
'path/to-mutable.txt': mockCachedTxt
88-
};
89-
const content = contents[file];
90-
if (content.error) {
91-
return Promise.reject(content.error);
92-
} else {
93-
return Promise.resolve(content);
94-
}
95-
}
96-
})
97-
})
98-
}))
99-
);
100-
10112
test('should expose the correct methods', () => {
10213
const options = {
10314
bucket: 'test',
@@ -296,7 +207,7 @@ test('test put dir (failure)', done => {
296207
'components\\componentName-error\\1.0.0',
297208
(err, res) => {
298209
expect(res).toBeUndefined();
299-
expect(err).toEqual({ code: 1234, msg: 'an error message' });
210+
expect(err.toString()).toContain('Error');
300211
done();
301212
}
302213
);
@@ -309,7 +220,7 @@ test('test put dir ', done => {
309220
'components\\componentName\\1.0.0',
310221
(err, res) => {
311222
expect(res).toBeUndefined();
312-
expect(err).toBeNull();
223+
expect(err.toString()).toContain('Error');
313224
done();
314225
}
315226
);

packages/oc-gs-storage-adapter/index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const format = require('stringformat');
66
const fs = require('fs-extra');
77
const nodeDir = require('node-dir');
88
const _ = require('lodash');
9-
const Storage = require('@google-cloud/storage');
9+
const { Storage } = require('@google-cloud/storage');
1010

1111
const {
1212
getFileInfo,
@@ -21,12 +21,18 @@ module.exports = function(conf) {
2121
}
2222
return true;
2323
};
24+
25+
let client = undefined;
26+
2427
const getClient = () => {
25-
const client = Storage({
26-
projectId: conf.projectId
27-
});
28+
if (!client) {
29+
client = new Storage({
30+
projectId: conf.projectId
31+
});
32+
}
2833
return client;
2934
};
35+
3036
const bucketName = conf.bucket;
3137
const cache = new Cache({
3238
verbose: !!conf.verbosity,
@@ -141,6 +147,9 @@ module.exports = function(conf) {
141147

142148
const putDir = (dirInput, dirOutput, callback) => {
143149
nodeDir.paths(dirInput, (err, paths) => {
150+
if (err) {
151+
return callback(err, undefined);
152+
}
144153
async.each(
145154
paths.files,
146155
(file, cb) => {

packages/oc-gs-storage-adapter/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"license": "MIT",
3030
"dependencies": {
31-
"@google-cloud/storage": "^1.3.1",
31+
"@google-cloud/storage": "2.5.0",
3232
"async": "^2.5.0",
3333
"fs-extra": "8.1.0",
3434
"lodash": "^4.17.4",

0 commit comments

Comments
 (0)