Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 71 additions & 65 deletions README.md

Large diffs are not rendered by default.

38 changes: 20 additions & 18 deletions lib/changesreader.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const EventEmitter = require('events').EventEmitter
import EventEmitter from 'node:events'
import stream from 'node:stream'
const AbortController = global.AbortController
const stream = require('stream')
const EVENT_BATCH = 'batch'
const EVENT_CHANGE = 'change'
const EVENT_SEQ = 'seq'
const EVENT_ERROR = 'error'

// streaming line breaker
const liner = () => {
function liner() {
const liner = new stream.Transform({ objectMode: true })

liner._transform = function (chunk, encoding, done) {
Expand All @@ -33,7 +33,7 @@ const liner = () => {
}

// streaming change processor
const changeProcessor = (ee, batchSize) => {
function changeProcessor(ee, batchSize) {
const changeProcessor = new stream.Transform({ objectMode: true })
const buffer = []
changeProcessor.lastSeq = '0'
Expand Down Expand Up @@ -81,7 +81,7 @@ const changeProcessor = (ee, batchSize) => {
* @param {String} db - Name of the database.
* @param {Function} request - Nano.relax
*/
class ChangesReader {
export default class ChangesReader {
// constructor
constructor (db, request) {
this.db = db
Expand Down Expand Up @@ -278,21 +278,23 @@ class ChangesReader {
}
const lin = liner()
const cp = changeProcessor(self.ee, self.batchSize)
self.request(req)
.on(EVENT_ERROR, (e) => {
self.ee.emit(EVENT_ERROR, e)
})
.pipe(lin)
.pipe(cp)
.on('finish', (lastSeq) => {
// the 'end' event was triggering before the last data event
setTimeout(() => {
self.ee.emit('end', cp.lastSeq)
}, 10)
})
setImmediate(async () => {
const s = await self.request(req)
s.on(EVENT_ERROR, (e) => {
self.ee.emit(EVENT_ERROR, e)
})
.pipe(lin)
.pipe(cp)
.on('finish', (lastSeq) => {
// the 'end' event was triggering before the last data event
setTimeout(() => {
self.ee.emit('end', cp.lastSeq)
}, 10)
})
})


return self.ee
}
}

module.exports = ChangesReader
4 changes: 1 addition & 3 deletions lib/cookie.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { URL } = require('url')

// a simple cookie jar
class CookieJar {
export default class CookieJar {
// create new empty cookie jar
constructor () {
this.jar = []
Expand Down Expand Up @@ -126,4 +125,3 @@ class CookieJar {
}
}

module.exports = CookieJar
3 changes: 1 addition & 2 deletions lib/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const DASHES = '--'
// generate the payload, boundary and header for a multipart/related request
// to upload binary attachments to CouchDB.
// https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html
class MultiPartFactory {
export default class MultiPartFactory {
// constructor
constructor (parts) {
// generate a unique id that forms the boundary between parts
Expand Down Expand Up @@ -50,4 +50,3 @@ class MultiPartFactory {
}
}

module.exports = MultiPartFactory
26 changes: 13 additions & 13 deletions lib/nano.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,10 @@ declare namespace nano {
list(params: DocumentListParams): Promise<DocumentListResponse<D>>;
/** List document from this database as a stream.
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/bulk-api.html#get--db-_all_docs} */
listAsStream(): NodeJS.ReadStream;
listAsStream(): Promise<NodeJS.ReadStream>;
/** List document from this database as a stream with options.
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/bulk-api.html#get--db-_all_docs} */
listAsStream(params: DocumentListParams): NodeJS.ReadStream;
listAsStream(params: DocumentListParams): Promise<NodeJS.ReadStream>;
/** Fetch a list of documents by _id.
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/bulk-api.html#post--db-_all_docs} */
fetch(docnames: BulkFetchDocsWrapper): Promise<DocumentFetchResponse<D>>;
Expand Down Expand Up @@ -482,7 +482,7 @@ declare namespace nano {
designname: string,
searchname: string,
params: DocumentSearchParams
): NodeJS.ReadStream;
): Promise<NodeJS.ReadStream>;
/** Low-level wrapper that executes a view from a Design Document.
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/ddoc/views.html#get--db-_design-ddoc-_view-view} */
baseView<V>(
Expand All @@ -509,14 +509,14 @@ declare namespace nano {
viewAsStream<V>(
designname: string,
viewname: string
): NodeJS.ReadStream;
): Promise<NodeJS.ReadStream>;
/** Executes a view from a Design Document, with options as a stream
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/ddoc/views.html#get--db-_design-ddoc-_view-view} */
viewAsStream<V>(
designname: string,
viewname: string,
params: DocumentViewParams
): NodeJS.ReadStream;
): Promise<NodeJS.ReadStream>;
/** Applies a list function to a view.
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/ddoc/render.html#db-design-design-doc-list-list-name-view-name} */
viewWithList(
Expand All @@ -538,21 +538,21 @@ declare namespace nano {
designname: string,
viewname: string,
listname: string
): NodeJS.ReadStream;
): Promise<NodeJS.ReadStream>;
/** Applies a list function to a view with options as a stream.
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/ddoc/render.html#db-design-design-doc-list-list-name-view-name} */
viewWithListAsStream(
designname: string,
viewname: string,
listname: string,
params: DocumentViewParams
): NodeJS.ReadStream;
): Promise<NodeJS.ReadStream>;
/** Run Mango query.
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/find.html#db-find} */
find(query: MangoQuery): Promise <MangoResponse<D>>;
/** Run Mango query as a stream.
* @see Docs: {@link http://docs.couchdb.org/en/latest/api/database/find.html#db-find} */
findAsStream(query: MangoQuery): NodeJS.ReadStream;
findAsStream(query: MangoQuery): Promise<NodeJS.ReadStream>;
/** Server scope */
server: ServerScope;
/** Fetch information about a single partition in this database.
Expand All @@ -563,13 +563,13 @@ declare namespace nano {
partitionedList(partitionKey: string, params?: DocumentFetchParams): Promise<DocumentListResponse<D>>;
/** List documents in a single partition in this database as a stream.
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/partitioned-dbs.html#db-partition-partition-all-docs} */
partitionedListAsStream(partitionKey: string, params?: DocumentFetchParams): NodeJS.ReadStream;
partitionedListAsStream(partitionKey: string, params?: DocumentFetchParams): Promise<NodeJS.ReadStream>;
/** Run Mango query a single partition in this database.
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/partitioned-dbs.html#db-partition-partition-id-find} */
partitionedFind(partitionKey: string, query: MangoQuery): Promise <MangoResponse<D>>;
/** Run Mango query a single partition in this database, as a stream.
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/partitioned-dbs.html#db-partition-partition-id-find} */
partitionedFindAsStream(partitionKey: string, query: MangoQuery): NodeJS.ReadStream;
partitionedFindAsStream(partitionKey: string, query: MangoQuery): Promise<NodeJS.ReadStream>;
/** Run a full-text search in a single partition in this database. */
partitionedSearch<V>(
partitionKey: string,
Expand All @@ -583,7 +583,7 @@ declare namespace nano {
designname: string,
searchname: string,
params: DocumentSearchParams
): NodeJS.ReadStream;
): Promise<NodeJS.ReadStream>;
/** Executes the specified view function in a single partition from the specified design document.
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/partitioned-dbs.html#db-partition-partition-design-design-doc-view-view-name} */
partitionedView<V>(
Expand All @@ -599,7 +599,7 @@ declare namespace nano {
designname: string,
viewname: string,
params: DocumentViewParams
): NodeJS.ReadStream;
): Promise<NodeJS.ReadStream>;
}

/** attachment data */
Expand Down Expand Up @@ -649,7 +649,7 @@ declare namespace nano {
get(docname: string, attname: string): Promise<Buffer>;
/** Get an attachment as a stream.
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/document/attachments.html#get--db-docid-attname} */
getAsStream(docname: string, attname: string): NodeJS.ReadStream;
getAsStream(docname: string, attname: string): Promise<NodeJS.ReadStream>;
/** Get an attachment with options.
* @see Docs: {@link https://docs.couchdb.org/en/latest/api/document/attachments.html#get--db-docid-attname} */
get(
Expand Down
Loading