{"record":{"id":"9b27b9bfb97d6732","repo":"parallax/jsPDF","slug":"invalid-context-passed-to-initialize-pubsub-jspdf","errorCode":null,"errorMessage":"Invalid Context passed to initialize PubSub (jsPDF-module)","messagePattern":"Invalid Context passed to initialize PubSub \\(jsPDF-module\\)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"src/jspdf.js","lineNumber":23,"sourceCode":"// @endif\nimport { globalObject } from \"./libs/globalObject.js\";\nimport { RGBColor } from \"./libs/rgbcolor.js\";\nimport { btoa } from \"./libs/AtobBtoa.js\";\nimport { console } from \"./libs/console.js\";\nimport { PDFSecurity } from \"./libs/pdfsecurity.js\";\nimport { toPDFName } from \"./libs/pdfname.js\";\n/**\n * jsPDF's Internal PubSub Implementation.\n * Backward compatible rewritten on 2014 by\n * Diego Casorran, https://github.com/diegocr\n *\n * @class\n * @name PubSub\n * @ignore\n */\nfunction PubSub(context) {\n  if (typeof context !== \"object\") {\n    throw new Error(\n      \"Invalid Context passed to initialize PubSub (jsPDF-module)\"\n    );\n  }\n  var topics = {};\n\n  this.subscribe = function(topic, callback, once) {\n    once = once || false;\n    if (\n      typeof topic !== \"string\" ||\n      typeof callback !== \"function\" ||\n      typeof once !== \"boolean\"\n    ) {\n      throw new Error(\n        \"Invalid arguments passed to PubSub.subscribe (jsPDF-module)\"\n      );\n    }\n\n    if (!topics.hasOwnProperty(topic)) {","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/jspdf.js#L5-L41","documentation":"The internal PubSub event bus rejects construction unless it is handed an object as its context. The guard `typeof context !== \"object\"` at src/jspdf.js:22 is a fail-fast invariant: PubSub needs a context to scope event handlers into. In production code the only call site is `new PubSub(API)` at src/jspdf.js:1063, where API is always an object literal, so a normal user almost never triggers this. It surfaces when code reaches into `jsPDF.API.__private__.PubSub` and instantiates it manually, or when the API object has been replaced/corrupted by a bundler, a bad plugin, or a monkey-patch that reassigned the internal API to a primitive.","triggerScenarios":"Calling `new jsPDF.API.__private__.PubSub(\"foo\")` / `new PubSub(42)` / `new PubSub(undefined)` directly; a plugin or bundler that reassigns the `API` object to a non-object before `new PubSub(API)` runs at src/jspdf.js:1063; a tree-shaking/optimization bug that drops the API object initialization leaving a primitive in its place.","commonSituations":"Custom plugins that manually re-instantiate the event bus; broken bundler output after a major jsPDF version upgrade where module internals were reorganized; test harnesses that stub out globals and accidentally leak a primitive into the constructor path; monkey-patching jsPDF internals (e.g. to intercept events) without preserving object identity.","solutions":["Do not construct PubSub yourself — it is an internal class created once per jsPDF instance; rely on the instance's own event API instead.","If you are patching internals, verify `typeof API === \"object\" && API !== null` before `new PubSub(API)` runs; `typeof null === \"object\"` so also guard against null.","On a version upgrade, diff the internal API surface (`API.__private__`) — if PubSub moved, update your plugin to use the new event hooks (`doc.internal.events`) rather than reconstructing the bus.","If the error appears without custom code, clear bundler caches (node_modules, webpack/vite cache) and rebuild — a corrupted internal object is the likely cause."],"exampleFix":"// before (broken: passing a primitive)\nvar bus = new jsPDF.API.__private__.PubSub('myEvents');\n\n// after: reuse the instance's own event bus\ndoc.internal.events.subscribe('addPage', function () { /* ... */ });","handlingStrategy":"type-guard","validationCode":"function isPubSubContext(ctx) {\n  return typeof ctx === 'object' && ctx !== null && typeof ctx.subscribe !== 'function' || ctx === undefined;\n}\n// only instantiate if valid; otherwise reuse the instance's own bus\nif (isPubSubContext(myApi)) {\n  new jsPDF.API.__private__.PubSub(myApi);\n}","typeGuard":"function isPubSubContext(ctx) {\n  return typeof ctx === 'object' && ctx !== null;\n}","tryCatchPattern":"try {\n  var bus = new jsPDF.API.__private__.PubSub(ctx);\n} catch (e) {\n  if (/Invalid Context/.test(e.message)) {\n    // fall back to the document's existing event bus\n    bus = doc.internal.events;\n  } else throw e;\n}","preventionTips":["Do not construct PubSub directly — use doc.internal.events on an existing jsPDF instance.","When patching internals, assert the API object is a non-null object before construction.","After upgrades, re-check that the internal event API hasn't moved."],"tags":["internal","pubsub","events","invariant"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}