From 24f9c28bfbc3bcc63509ca37a909600776ce95f6 Mon Sep 17 00:00:00 2001 From: Erich Duda Date: Thu, 27 Jul 2017 17:59:01 +0200 Subject: [PATCH] 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 #463 --- lib/Repository.js | 15 ++++++++++++--- test/repository.spec.js | 10 ++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index d338b261..7d8908eb 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -245,7 +245,7 @@ class Repository extends Requestable { /** * Create a blob * @see https://developer.github.com/v3/git/blobs/#create-a-blob - * @param {(string|Buffer|Blob)} content - the content to add to the repository + * @param {(string|Buffer|Blob|Object)} content - the content to add to the repository * @param {Requestable.callback} cb - will receive the details of the created blob * @return {Promise} - the promise for the http request */ @@ -258,7 +258,7 @@ class Repository extends Requestable { /** * Get the object that represents the provided content - * @param {string|Buffer|Blob} content - the content to send to the server + * @param {string|Buffer|Blob|Object} content - the content to send to the server * @return {Object} the representation of `content` for the GitHub API */ _getContentObject(content) { @@ -283,9 +283,18 @@ class Repository extends Requestable { encoding: 'base64', }; + } else if (typeof content === 'object') { + log('content is an object'); + + if (typeof content.content !== 'string') { + throw new Error('The object must contain content item of type string.'); + } + + return content; + } else { // eslint-disable-line log(`Not sure what this content is: ${typeof content}, ${JSON.stringify(content)}`); - throw new Error('Unknown content passed to postBlob. Must be string or Buffer (node) or Blob (web)'); + throw new Error('Unknown content passed to postBlob. Must be string or Buffer (node) or Blob (web) or Object'); } } diff --git a/test/repository.spec.js b/test/repository.spec.js index ef5101cf..1fcb0238 100644 --- a/test/repository.spec.js +++ b/test/repository.spec.js @@ -578,6 +578,16 @@ describe('Repository', function() { remoteRepo.createBlob(imageBlob, assertSuccessful(done)); }); + it('should read the same unicode string blob as it was written', function(done) { + let content = 'The temperature is 25°C' + remoteRepo.createBlob({content: content, encoding: 'utf-8'}, assertSuccessful(done, function(_, resp) { + remoteRepo.getBlob(resp.sha, assertSuccessful(done, function(_, returnedContent) { + expect(returnedContent).to.be(content); + done(); + })); + })); + }); + it('should star the repo', function(done) { remoteRepo.star(assertSuccessful(done, function() { remoteRepo.isStarred(assertSuccessful(done)); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy