You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by gl...@apache.org on 2020/02/27 16:09:58 UTC

[couchdb-nano] 04/12: alter the way attachment.insertAsStream works

This is an automated email from the ASF dual-hosted git repository.

glynnbird pushed a commit to branch axios
in repository https://gitbox.apache.org/repos/asf/couchdb-nano.git

commit d60b9b04547d922a44aa825ef09045201f9b2153
Author: Glynn Bird <gl...@gmail.com>
AuthorDate: Thu Feb 13 16:11:18 2020 +0000

    alter the way attachment.insertAsStream works
---
 README.md                              |  7 ++-----
 lib/nano.d.ts                          |  8 ++++----
 lib/nano.js                            |  1 -
 test/attachment.insertAsStream.test.js | 22 ++++++----------------
 4 files changed, 12 insertions(+), 26 deletions(-)

diff --git a/README.md b/README.md
index d5cd8bd..f383a3a 100644
--- a/README.md
+++ b/README.md
@@ -953,11 +953,8 @@ It may be more memory-efficient to pipe a stream of data from a source (file, ne
 
 ```js
   const rs = fs.createReadStream('logo.png');
-  const is = db.attachment.insertAsStream('mydoc', 'logo.png', null, 'image/png',
-    { rev: '12-150985a725ec88be471921a54ce91452' }).on('end', () => {
-      console.log('done')
-  });
-  rs.pipe(is);
+  const reply = await db.attachment.insertAsStream('mydoc', 'logo.png', rs, 'image/png',
+    { rev: '12-150985a725ec88be471921a54ce91452' })
 ```
 
 ### db.attachment.get(docname, attname, [params], [callback])
diff --git a/lib/nano.d.ts b/lib/nano.d.ts
index 4de9a95..e93f419 100644
--- a/lib/nano.d.ts
+++ b/lib/nano.d.ts
@@ -363,16 +363,16 @@ declare namespace nano {
     insertAsStream(
       docname: string,
       attname: string,
-      att: any,
+      att: NodeJS.ReadReadStream,
       contenttype: string
-    ): Request;
+    ): Promise<DocumentInsertResponse>;
     insertAsStream(
       docname: string,
       attname: string,
-      att: any,
+      att: NodeJS.ReadReadStream,
       contenttype: string,
       params: any
-    ): Request
+    ): Promise<DocumentInsertResponse>
     get(docname: string, attname: string, callback?: Callback<Buffer>): Promise<Buffer>;
     getAsStream(docname: string, attname: string): Request;
     get(
diff --git a/lib/nano.js b/lib/nano.js
index 9268207..6603736 100644
--- a/lib/nano.js
+++ b/lib/nano.js
@@ -878,7 +878,6 @@ module.exports = exports = function dbScope (cfg) {
         doc: docName,
         qs: opts,
         body: att,
-        stream: true,
         dontStringify: true
       })
     }
diff --git a/test/attachment.insertAsStream.test.js b/test/attachment.insertAsStream.test.js
index f7a3022..2d196ff 100644
--- a/test/attachment.insertAsStream.test.js
+++ b/test/attachment.insertAsStream.test.js
@@ -30,7 +30,7 @@ afterEach(() => {
   nock.cleanAll()
 })
 
-test('should be able to insert document attachment as stream - PUT /db/docname/attachment - db.attachment.insertAsStream', () => {
+test('should be able to insert document attachment as stream - PUT /db/docname/attachment - db.attachment.insertAsStream', async () => {
   // mocks
   const response = { ok: true, id: 'docname', rev: '2-456' }
   const scope = nock(COUCH_URL, { reqheaders: { 'content-type': 'image/jpg' } })
@@ -38,19 +38,9 @@ test('should be able to insert document attachment as stream - PUT /db/docname/a
     .reply(200, response)
 
   // test PUT /db/docname/attachment
-  return new Promise((resolve, reject) => {
-    const rs = fs.createReadStream('./test/logo.jpg')
-    const db = nano.db.use('db')
-    let reply = ''
-    const is = db.attachment.insertAsStream('docname', 'logo.jpg', null, 'image/jpg', { rev: '1-150' })
-      .on('data', (data) => {
-        reply += data.toString()
-      })
-      .on('end', () => {
-        expect(reply).toStrictEqual(JSON.stringify(response))
-        expect(scope.isDone()).toBe(true)
-        resolve()
-      })
-    rs.pipe(is)
-  })
+  const rs = fs.createReadStream('./test/logo.jpg')
+  const db = nano.db.use('db')
+  const reply = await db.attachment.insertAsStream('docname', 'logo.jpg', rs, 'image/jpg', { rev: '1-150' })
+  expect(reply).toStrictEqual(response)
+  expect(scope.isDone()).toBe(true)
 })