@@ -17,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
1717 return to ;
1818} ;
1919var __toESM = ( mod , isNodeMode , target ) => ( target = mod != null ? __create ( __getProtoOf ( mod ) ) : { } , __copyProps (
20+ // If the importer is in node compatibility mode or this is not an ESM
21+ // file that has been converted to a CommonJS file using a Babel-
22+ // compatible transform (i.e. "__esModule" has not been set), then set
23+ // "default" to the CommonJS "module.exports" for node compatibility.
2024 isNodeMode || ! mod || ! mod . __esModule ? __defProp ( target , "default" , { value : mod , enumerable : true } ) : target ,
2125 mod
2226) ) ;
@@ -191,15 +195,13 @@ function parseFieldBitmaps(allFields, fieldBitmapsAsHex) {
191195 }
192196 let fieldNames = [ ] ;
193197 if ( fieldBitmapsAsHex [ 0 ] . startsWith ( "0x" ) ) {
194- fieldNames = fieldNames . concat (
195- getFieldNamesFromBitmap ( allFields , fieldBitmapsAsHex [ 0 ] )
196- ) ;
198+ fieldNames = getFieldNamesFromBitmap ( allFields , fieldBitmapsAsHex [ 0 ] ) ;
197199 }
198- if ( fieldBitmapsAsHex [ fieldBitmapsAsHex . length - 1 ] . indexOf ( "-" ) !== - 1 ) {
200+ if ( fieldBitmapsAsHex . length > 1 && fieldBitmapsAsHex [ fieldBitmapsAsHex . length - 1 ] . indexOf ( "-" ) !== - 1 ) {
199201 fieldBitmapsAsHex . forEach ( ( fieldBitmapAsHex ) => {
200202 const bitmapMapStrings = fieldBitmapAsHex . split ( "-" ) ;
201203 if ( bitmapMapStrings . length >= 2 ) {
202- const parentField = allFields [ parseInt ( bitmapMapStrings [ 0 ] ) ] ;
204+ const parentField = allFields [ parseInt ( bitmapMapStrings [ 0 ] , 10 ) ] ;
203205 const childFields = getChildFields ( parentField ) ;
204206 const childFieldNames = getFieldNamesFromBitmap (
205207 childFields ,
@@ -221,15 +223,13 @@ function getChildFields(parentField) {
221223 types . forEach ( ( type ) => {
222224 if ( type instanceof import_avro_js . default . types . RecordType ) {
223225 fields = fields . concat ( type . getFields ( ) ) ;
224- } else if ( type instanceof import_avro_js . default . types . NullType ) {
225- fields . push ( null ) ;
226226 }
227227 } ) ;
228228 return fields ;
229229}
230230function getFieldNamesFromBitmap ( fields , fieldBitmapAsHex ) {
231231 let binValue = hexToBin ( fieldBitmapAsHex ) ;
232- binValue = reverseBytes ( binValue ) ;
232+ binValue = binValue . split ( "" ) . reverse ( ) . join ( "" ) ;
233233 const fieldNames = [ ] ;
234234 for ( let i = 0 ; i < binValue . length && i < fields . length ; i ++ ) {
235235 if ( binValue [ i ] === "1" ) {
@@ -238,13 +238,6 @@ function getFieldNamesFromBitmap(fields, fieldBitmapAsHex) {
238238 }
239239 return fieldNames ;
240240}
241- function reverseBytes ( input ) {
242- let output = "" ;
243- for ( let i = input . length / 8 - 1 ; i >= 0 ; i -- ) {
244- output += input . substring ( i * 8 , ( i + 1 ) * 8 ) ;
245- }
246- return output ;
247- }
248241function decodeReplayId ( encodedReplayId ) {
249242 return Number ( encodedReplayId . readBigUInt64BE ( ) ) ;
250243}
@@ -280,6 +273,10 @@ var import_jsforce = __toESM(require("jsforce"), 1);
280273var import_undici = require ( "undici" ) ;
281274var _authWithUsernamePassword , authWithUsernamePassword_fn , _authWithOAuthClientCredentials , authWithOAuthClientCredentials_fn , _authWithJwtBearer , authWithJwtBearer_fn , _authWithOAuth , authWithOAuth_fn ;
282275var _SalesforceAuth = class {
276+ /**
277+ * Authenticates with the auth mode specified in configuration
278+ * @returns {ConnectionMetadata }
279+ */
283280 static async authenticate ( ) {
284281 var _a , _b , _c ;
285282 if ( Configuration . isUsernamePasswordAuth ( ) ) {
@@ -374,9 +371,26 @@ authWithOAuth_fn = async function(body) {
374371 username : preferred_username
375372 } ;
376373} ;
374+ /**
375+ * Authenticates with the username/password flow
376+ * @returns {ConnectionMetadata }
377+ */
377378__privateAdd ( SalesforceAuth , _authWithUsernamePassword ) ;
379+ /**
380+ * Authenticates with the OAuth 2.0 client credentials flow
381+ * @returns {ConnectionMetadata }
382+ */
378383__privateAdd ( SalesforceAuth , _authWithOAuthClientCredentials ) ;
384+ /**
385+ * Authenticates with the OAuth 2.0 JWT bearer flow
386+ * @returns {ConnectionMetadata }
387+ */
379388__privateAdd ( SalesforceAuth , _authWithJwtBearer ) ;
389+ /**
390+ * Generic OAuth 2.0 connect method
391+ * @param {string } body URL encoded body
392+ * @returns {ConnectionMetadata } connection metadata
393+ */
380394__privateAdd ( SalesforceAuth , _authWithOAuth ) ;
381395function base64url ( input ) {
382396 const buf = Buffer . from ( input , "utf8" ) ;
@@ -385,9 +399,21 @@ function base64url(input) {
385399
386400// src/client.js
387401var PubSubApiClient = class {
402+ /**
403+ * gRPC client
404+ * @type {Object }
405+ */
388406 #client;
407+ /**
408+ * Map of schemas indexed by topic name
409+ * @type {Map<string,Schema> }
410+ */
389411 #schemaChache;
390412 #logger;
413+ /**
414+ * Builds a new Pub/Sub API client
415+ * @param {Logger } logger an optional custom logger. The client uses the console if no value is supplied.
416+ */
391417 constructor ( logger = console ) {
392418 this . #logger = logger ;
393419 this . #schemaChache = /* @__PURE__ */ new Map ( ) ;
@@ -400,6 +426,10 @@ var PubSubApiClient = class {
400426 } ) ;
401427 }
402428 }
429+ /**
430+ * Authenticates with Salesforce then, connects to the Pub/Sub API
431+ * @returns {Promise<void> } Promise that resolves once the connection is established
432+ */
403433 async connect ( ) {
404434 if ( Configuration . isUserSuppliedAuth ( ) ) {
405435 throw new Error (
@@ -419,6 +449,14 @@ var PubSubApiClient = class {
419449 }
420450 return this . #connectToPubSubApi( conMetadata ) ;
421451 }
452+ /**
453+ * Connects to the Pub/Sub API with user-supplied authentication
454+ * @param {string } accessToken
455+ * @param {string } instanceUrl
456+ * @param {string } organizationId
457+ * @param {string } username
458+ * @returns {Promise<void> } Promise that resolves once the connection is established
459+ */
422460 async connectWithAuth ( accessToken , instanceUrl , organizationId , username ) {
423461 return this . #connectToPubSubApi( {
424462 accessToken,
@@ -427,6 +465,11 @@ var PubSubApiClient = class {
427465 username
428466 } ) ;
429467 }
468+ /**
469+ * Connects to the Pub/Sub API
470+ * @param {import('./auth.js').ConnectionMetadata } conMetadata
471+ * @returns {Promise<void> } Promise that resolves once the connection is established
472+ */
430473 async #connectToPubSubApi( conMetadata ) {
431474 try {
432475 const rootCert = import_fs2 . default . readFileSync ( import_certifi . default ) ;
@@ -461,13 +504,27 @@ var PubSubApiClient = class {
461504 } ) ;
462505 }
463506 }
507+ /**
508+ * Subscribes to a topic and retrieves all past events in retention window
509+ * @param {string } topicName name of the topic that we're subscribing to
510+ * @param {number } numRequested number of events requested
511+ * @param {number } replayId replay ID
512+ * @returns {Promise<EventEmitter> } Promise that holds an emitter that allows you to listen to received events and stream lifecycle events
513+ */
464514 async subscribeFromEarliestEvent ( topicName , numRequested ) {
465515 return this . #subscribe( {
466516 topicName,
467517 numRequested,
468518 replayPreset : 1
469519 } ) ;
470520 }
521+ /**
522+ * Subscribes to a topic and retrieve past events starting from a replay ID
523+ * @param {string } topicName name of the topic that we're subscribing to
524+ * @param {number } numRequested number of events requested
525+ * @param {number } replayId replay ID
526+ * @returns {Promise<EventEmitter> } Promise that holds an emitter that allows you to listen to received events and stream lifecycle events
527+ */
471528 async subscribeFromReplayId ( topicName , numRequested , replayId ) {
472529 return this . #subscribe( {
473530 topicName,
@@ -476,12 +533,24 @@ var PubSubApiClient = class {
476533 replayId : encodeReplayId ( replayId )
477534 } ) ;
478535 }
536+ /**
537+ * Subscribes to a topic
538+ * @param {string } topicName name of the topic that we're subscribing to
539+ * @param {number } numRequested number of events requested
540+ * @returns {Promise<EventEmitter> } Promise that holds an emitter that allows you to listen to received events and stream lifecycle events
541+ */
479542 async subscribe ( topicName , numRequested ) {
480543 return this . #subscribe( {
481544 topicName,
482545 numRequested
483546 } ) ;
484547 }
548+ /**
549+ * Subscribes to a topic using the gRPC client and an event schema
550+ * @param {string } topicName name of the topic that we're subscribing to
551+ * @param {number } numRequested number of events requested
552+ * @return {EventEmitter } emitter that allows you to listen to received events and stream lifecycle events
553+ */
485554 async #subscribe( subscribeRequest ) {
486555 try {
487556 if ( ! this . #client) {
@@ -534,6 +603,13 @@ var PubSubApiClient = class {
534603 ) ;
535604 }
536605 }
606+ /**
607+ * Publishes a payload to a topic using the gRPC client
608+ * @param {string } topicName name of the topic that we're subscribing to
609+ * @param {Object } payload
610+ * @param {string } [correlationKey] optional correlation key. If you don't provide one, we'll generate a random UUID for you.
611+ * @returns {Promise<PublishResult> } Promise holding a PublishResult object with replayId and correlationKey
612+ */
537613 async publish ( topicName , payload , correlationKey ) {
538614 try {
539615 if ( ! this . #client) {
@@ -548,6 +624,7 @@ var PubSubApiClient = class {
548624 events : [
549625 {
550626 id,
627+ // Correlation key
551628 schemaId : schema . id ,
552629 payload : schema . type . toBuffer ( payload )
553630 }
@@ -571,10 +648,19 @@ var PubSubApiClient = class {
571648 } ) ;
572649 }
573650 }
651+ /**
652+ * Closes the gRPC connection. The client will no longer receive events for any topic.
653+ */
574654 close ( ) {
575655 this . #logger. info ( "closing gRPC stream" ) ;
576656 this . #client. close ( ) ;
577657 }
658+ /**
659+ * Retrieves the event schema for a topic from the cache.
660+ * If it's not cached, fetches the shema with the gRPC client.
661+ * @param {string } topicName name of the topic that we're fetching
662+ * @returns {Promise<Schema> } Promise holding parsed event schema
663+ */
578664 async #getEventSchema( topicName ) {
579665 let schema = this . #schemaChache. get ( topicName ) ;
580666 if ( ! schema ) {
@@ -590,6 +676,11 @@ var PubSubApiClient = class {
590676 }
591677 return schema ;
592678 }
679+ /**
680+ * Requests the event schema for a topic using the gRPC client
681+ * @param {string } topicName name of the topic that we're fetching
682+ * @returns {Promise<Schema> } Promise holding parsed event schema
683+ */
593684 async #fetchEventSchemaWithClient( topicName ) {
594685 return new Promise ( ( resolve , reject ) => {
595686 this . #client. GetTopic ( { topicName } , ( topicError , response ) => {
0 commit comments