Skip to content

Commit 4047fd9

Browse files
Merge pull request #314 from qbikez/fix-double-callback
Eliminate the possibilty of invoking callback twice.
2 parents 1a41d23 + 8e2e31a commit 4047fd9

File tree

1 file changed

+67
-57
lines changed
  • packages/oc-azure-storage-adapter

1 file changed

+67
-57
lines changed

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

Lines changed: 67 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,16 @@ module.exports = function (conf) {
9191
return callback(err);
9292
}
9393

94+
let parsed = null;
9495
try {
95-
callback(null, JSON.parse(file));
96+
parsed = JSON.parse(file);
9697
} catch (er) {
9798
return callback({
9899
code: strings.errors.STORAGE.FILE_NOT_VALID_CODE,
99100
msg: format(strings.errors.STORAGE.FILE_NOT_VALID, filePath)
100101
});
101102
}
103+
callback(null, parsed);
102104
});
103105
};
104106

@@ -205,76 +207,84 @@ module.exports = function (conf) {
205207
};
206208

207209
const putFileContent = (fileContent, fileName, isPrivate, callback) => {
208-
const fileInfo = getFileInfo(fileName);
209-
const contentSettings = {
210-
cacheControl: 'public, max-age=31556926'
211-
};
212-
if (fileInfo.mimeType) {
213-
contentSettings.contentType = fileInfo.mimeType;
214-
}
210+
try {
211+
const fileInfo = getFileInfo(fileName);
212+
const contentSettings = {
213+
cacheControl: 'public, max-age=31556926'
214+
};
215+
if (fileInfo.mimeType) {
216+
contentSettings.contentType = fileInfo.mimeType;
217+
}
215218

216-
if (fileInfo.gzip) {
217-
contentSettings.contentEncoding = 'gzip';
218-
}
219+
if (fileInfo.gzip) {
220+
contentSettings.contentEncoding = 'gzip';
221+
}
222+
223+
const uploadToAzureContainer = (rereadable, containerName, cb) => {
224+
try {
225+
if (fileContent instanceof stream.Stream) {
226+
return fileContent.pipe(
227+
getClient().createWriteStreamToBlockBlob(
228+
containerName,
229+
fileName,
230+
{ contentSettings: contentSettings },
231+
(err, res) => {
232+
if (rereadable) {
233+
// I need a fresh read stream and this is the only thing I came up with
234+
// very ugly and has poor performance, but works
235+
fileContent = getClient().createReadStream(
236+
containerName,
237+
fileName
238+
);
239+
}
240+
241+
cb(err, res);
242+
}
243+
)
244+
);
245+
}
219246

220-
const uploadToAzureContainer = (rereadable, containerName, cb) => {
221-
if (fileContent instanceof stream.Stream) {
222-
return fileContent.pipe(
223-
getClient().createWriteStreamToBlockBlob(
247+
getClient().createBlockBlobFromText(
224248
containerName,
225249
fileName,
250+
fileContent,
226251
{ contentSettings: contentSettings },
227-
(err, res) => {
228-
if (rereadable) {
229-
// I need a fresh read stream and this is the only thing I came up with
230-
// very ugly and has poor performance, but works
231-
fileContent = getClient().createReadStream(
232-
containerName,
233-
fileName
234-
);
235-
}
236-
237-
cb(err, res);
238-
}
239-
)
240-
);
241-
}
252+
cb
253+
);
254+
} catch (err) {
255+
return cb(err);
256+
}
257+
};
242258

243-
getClient().createBlockBlobFromText(
244-
containerName,
245-
fileName,
246-
fileContent,
247-
{ contentSettings: contentSettings },
248-
cb
249-
);
250-
};
259+
const makeReReadable = !isPrivate;
260+
uploadToAzureContainer(
261+
makeReReadable,
262+
conf.privateContainerName,
263+
(err, result) => {
264+
if (err) {
265+
return callback(err);
266+
}
251267

252-
const makeReReadable = !isPrivate;
253-
uploadToAzureContainer(
254-
makeReReadable,
255-
conf.privateContainerName,
256-
(err, result) => {
257-
if (err) {
258-
return callback(err);
259-
}
268+
if (!isPrivate) {
269+
return uploadToAzureContainer(
270+
false,
271+
conf.publicContainerName,
272+
callback
273+
);
274+
}
260275

261-
if (!isPrivate) {
262-
return uploadToAzureContainer(
263-
false,
264-
conf.publicContainerName,
265-
callback
266-
);
276+
return callback(null, result);
267277
}
268-
269-
return callback(null, result);
270-
}
271-
);
278+
);
279+
} catch (err) {
280+
return callback(err);
281+
}
272282
};
273283

274284
const putFile = (filePath, fileName, isPrivate, callback) => {
275285
try {
276286
const stream = fs.createReadStream(filePath);
277-
return putFileContent(stream, fileName, isPrivate, callback);
287+
putFileContent(stream, fileName, isPrivate, callback);
278288
} catch (e) {
279289
return callback(e);
280290
}

0 commit comments

Comments
 (0)