Skip to content

Commit 24f9c28

Browse files
committed
Enable to pass an object to createBlob
createBlob automatically encodes string parameter by Utf8 library. In some cases the string value can be already encoded in Utf8 so the encoding should be skipped. This commit enables to pass object containing the content and encoding items. This object is directly sent to Github. Users have better control about what is actually sending. closes github-tools#463
1 parent 64a9223 commit 24f9c28

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/Repository.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class Repository extends Requestable {
245245
/**
246246
* Create a blob
247247
* @see https://developer.github.com/v3/git/blobs/#create-a-blob
248-
* @param {(string|Buffer|Blob)} content - the content to add to the repository
248+
* @param {(string|Buffer|Blob|Object)} content - the content to add to the repository
249249
* @param {Requestable.callback} cb - will receive the details of the created blob
250250
* @return {Promise} - the promise for the http request
251251
*/
@@ -258,7 +258,7 @@ class Repository extends Requestable {
258258

259259
/**
260260
* Get the object that represents the provided content
261-
* @param {string|Buffer|Blob} content - the content to send to the server
261+
* @param {string|Buffer|Blob|Object} content - the content to send to the server
262262
* @return {Object} the representation of `content` for the GitHub API
263263
*/
264264
_getContentObject(content) {
@@ -283,9 +283,18 @@ class Repository extends Requestable {
283283
encoding: 'base64',
284284
};
285285

286+
} else if (typeof content === 'object') {
287+
log('content is an object');
288+
289+
if (typeof content.content !== 'string') {
290+
throw new Error('The object must contain content item of type string.');
291+
}
292+
293+
return content;
294+
286295
} else { // eslint-disable-line
287296
log(`Not sure what this content is: ${typeof content}, ${JSON.stringify(content)}`);
288-
throw new Error('Unknown content passed to postBlob. Must be string or Buffer (node) or Blob (web)');
297+
throw new Error('Unknown content passed to postBlob. Must be string or Buffer (node) or Blob (web) or Object');
289298
}
290299
}
291300

test/repository.spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,16 @@ describe('Repository', function() {
578578
remoteRepo.createBlob(imageBlob, assertSuccessful(done));
579579
});
580580

581+
it('should read the same unicode string blob as it was written', function(done) {
582+
let content = 'The temperature is 25°C'
583+
remoteRepo.createBlob({content: content, encoding: 'utf-8'}, assertSuccessful(done, function(_, resp) {
584+
remoteRepo.getBlob(resp.sha, assertSuccessful(done, function(_, returnedContent) {
585+
expect(returnedContent).to.be(content);
586+
done();
587+
}));
588+
}));
589+
});
590+
581591
it('should star the repo', function(done) {
582592
remoteRepo.star(assertSuccessful(done, function() {
583593
remoteRepo.isStarred(assertSuccessful(done));

0 commit comments

Comments
 (0)