Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
nodejs 22.16.0
nodejs 22.16.0
python 3.13.5
77 changes: 77 additions & 0 deletions examples/websocket_example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<!doctype html>
<html>
<head>
<title>AMQP websockets example</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script src="../dist/index.umd.js"></script>
</head>

<body>
<input type="text" id="request" style="width: 100%" />
<script type="module">
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
const testExchange = `test-exchange-browser-example`
const testQueue = `test-queue-browser-example`
const routingKey = `test-key-browser-example`
console.log("Creating the environment through a websocket...")
const environment = RabbitmqAmqpClient.createEnvironment({
host: "localhost",
port: 3000,
username: "rabbit",
password: "rabbit",
webSocket: { implementation: WebSocket },
})

console.log("Opening a connection...")
const connection = await environment.createConnection()
const management = connection.management()

console.log("Creating a queue and an exchange...")
const queue = await management.declareQueue(testQueue)
const exchange = await management.declareExchange(testExchange)

console.log("Binding exchange to queue...")
await management.bind(routingKey, { source: exchange, destination: queue })

console.log("Opening a publisher and publishing 10 messages...")
const publisher = await connection.createPublisher({ exchange: { name: testExchange, routingKey: routingKey } })
for (const i of Array(10).keys()) {
const publishResult = await publisher.publish(RabbitmqAmqpClient.createAmqpMessage({ body: `Hello - ${i} - ` }))
switch (publishResult.outcome) {
case RabbitmqAmqpClient.OutcomeState.ACCEPTED:
console.log("Message Accepted")
break
case RabbitmqAmqpClient.OutcomeState.RELEASED:
console.log("Message Released")
break
case RabbitmqAmqpClient.OutcomeState.REJECTED:
console.log("Message Rejected")
break
default:
break
}
}
publisher.close()

console.log("Opening a consumer and consuming messages...")
const consumer = await connection.createConsumer({
queue: { name: testQueue },
messageHandler: (context, msg) => {
context.accept()
console.log(`MessageId: ${msg.message_id}; Payload: ${msg.body}`)
},
})
consumer.start()
await sleep(5000)

console.log("Cleaning up...")
consumer.close()
await management.unbind(routingKey, { source: exchange, destination: queue })
await management.deleteExchange(testExchange)
await management.deleteQueue(testQueue)
management.close()
await connection.close()
await environment.close()
</script>
</body>
</html>
4,955 changes: 3,483 additions & 1,472 deletions package-lock.json

Large diffs are not rendered by default.

32 changes: 21 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,37 @@
"name": "rabbitmq-amqp-js-client",
"version": "0.3.2",
"description": "Rabbit AMQP 1.0 client for JS/TS application",
"main": "dist/index.js",
"main": "dist/index.cjs.js",
"module": "dist/index.es.js",
"types": "dist/index.d.ts",
"type": "module",
"exports": {
".": {
"browser": "./dist/index.umd.js",
"import": {
"types": "./dist/index.d.ts",
"default": "./dist/index.mjs"
"default": "./dist/index.es.js"
},
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
"default": "./dist/index.cjs.js"
},
"default": "./dist/index.mjs"
"default": "./dist/index.es.js"
},
"./dist/*": {
"types": "./dist/*.d.ts",
"import": "./dist/*.mjs",
"require": "./dist/*.cjs"
"import": "./dist/*.es.js",
"require": "./dist/*.cjs.js"
}
},
"scripts": {
"test": "vitest run",
"build": "tsc --project tsconfig.build.json && tsup",
"build": "npm run build:node && npm run build:browser",
"build:node": "vite build --config vite.config.ts",
"build:browser": "vite build --config vite.config.browser.ts",
"build:clean": "rm -rf dist && npm run build",
"check": "npm run check-ts && npm run check-lint && npm run check-format && npm run check-spell",
"check-ts": "tsc --noEmit",
"check-lint": "tsc --noEmit && eslint 'src/**/*.ts' 'test/**/*.ts'",
"check-ts": "tsc --project tsconfig.build.json --noEmit",
"check-lint": "eslint 'src/**/*.ts' 'test/**/*.ts'",
"check-format": "prettier -c 'src/**/*.ts' 'test/**/*.ts'",
"check-spell": "cspell 'src/**/*ts' 'test/**/*ts'",
"format": "prettier -w './**/*.ts'"
Expand All @@ -48,6 +53,9 @@
"homepage": "https://github.com/coders51/rabbitmq-amqp-js-client#readme",
"devDependencies": {
"@eslint/js": "^9.28.0",
"@rollup/plugin-commonjs": "^28.0.6",
"@rollup/plugin-node-resolve": "^16.0.1",
"@rollup/plugin-typescript": "^12.1.4",
"@tsconfig/node22": "^22.0.2",
"@types/jsonwebtoken": "^9.0.10",
"assertion-error": "^2.0.1",
Expand All @@ -57,9 +65,11 @@
"got": "^14.4.7",
"jsonwebtoken": "^9.0.2",
"prettier": "3.5.3",
"tsup": "^8.5.0",
"typescript": "^5.8.3",
"typescript-eslint": "^8.33.1",
"vite": "^7.1.7",
"vite-plugin-dts": "^4.5.4",
"vite-plugin-node-polyfills": "^0.24.0",
"vitest": "^3.2.1"
},
"dependencies": {
Expand Down
27 changes: 20 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
export { Management, AmqpManagement } from "./management.js"
export { createEnvironment, Environment, AmqpEnvironment } from "./environment.js"
export { Connection, AmqpConnection } from "./connection.js"
export { Publisher, AmqpPublisher } from "./publisher.js"
export { Consumer, AmqpConsumer } from "./consumer.js"
export { createAmqpMessage } from "./message.js"
export { OutcomeState, Offset, OffsetType } from "./utils.js"
import { AmqpManagement } from "./management.js"
export { AmqpManagement }
import { createEnvironment, AmqpEnvironment } from "./environment.js"
export { createEnvironment, AmqpEnvironment }
import { AmqpConnection } from "./connection.js"
export { AmqpConnection }
import { AmqpPublisher } from "./publisher.js"
export { AmqpPublisher }
import { AmqpConsumer } from "./consumer.js"
export { AmqpConsumer }
import { createAmqpMessage } from "./message.js"
export { createAmqpMessage }
import { OutcomeState, Offset, OffsetType } from "./utils.js"
export { OutcomeState, Offset, OffsetType }

export type { Management } from "./management.js"
export type { Environment } from "./environment.js"
export type { Connection } from "./connection.js"
export type { Publisher } from "./publisher.js"
export type { Consumer } from "./consumer.js"
93 changes: 93 additions & 0 deletions src/index_browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { AmqpManagement } from "./management.js"
import { createEnvironment, AmqpEnvironment } from "./environment.js"
import { AmqpConnection } from "./connection.js"
import { AmqpPublisher } from "./publisher.js"
import { AmqpConsumer } from "./consumer.js"
import { createAmqpMessage } from "./message.js"
import { OutcomeState, Offset, OffsetType } from "./utils.js"

import type { Management } from "./management.js"
import type { Environment } from "./environment.js"
import type { Connection } from "./connection.js"
import type { Publisher } from "./publisher.js"
import type { Consumer } from "./consumer.js"

declare global {
interface Window {
AmqpManagement: typeof AmqpManagement
createEnvironment: typeof createEnvironment
AmqpEnvironment: typeof AmqpEnvironment
AmqpConnection: typeof AmqpConnection
AmqpPublisher: typeof AmqpPublisher
AmqpConsumer: typeof AmqpConsumer
createAmqpMessage: typeof createAmqpMessage
OutcomeState: typeof OutcomeState
Offset: typeof Offset
OffsetType: typeof OffsetType
}

interface GlobalThis {
AmqpManagement: typeof AmqpManagement
createEnvironment: typeof createEnvironment
AmqpEnvironment: typeof AmqpEnvironment
AmqpConnection: typeof AmqpConnection
AmqpPublisher: typeof AmqpPublisher
AmqpConsumer: typeof AmqpConsumer
createAmqpMessage: typeof createAmqpMessage
OutcomeState: typeof OutcomeState
Offset: typeof Offset
OffsetType: typeof OffsetType
}

const AmqpManagement: typeof import("./management.js").AmqpManagement
const createEnvironment: typeof import("./environment.js").createEnvironment
const AmqpEnvironment: typeof import("./environment.js").AmqpEnvironment
const AmqpConnection: typeof import("./connection.js").AmqpConnection
const AmqpPublisher: typeof import("./publisher.js").AmqpPublisher
const AmqpConsumer: typeof import("./consumer.js").AmqpConsumer
const createAmqpMessage: typeof import("./message.js").createAmqpMessage
const OutcomeState: typeof import("./utils.js").OutcomeState
const Offset: typeof import("./utils.js").Offset
const OffsetType: typeof import("./utils.js").OffsetType
}

if (typeof globalThis !== "undefined") {
const global = globalThis as unknown as GlobalThis
global.AmqpManagement = AmqpManagement
global.createEnvironment = createEnvironment
global.AmqpEnvironment = AmqpEnvironment
global.AmqpConnection = AmqpConnection
global.AmqpPublisher = AmqpPublisher
global.AmqpConsumer = AmqpConsumer
global.createAmqpMessage = createAmqpMessage
global.OutcomeState = OutcomeState
global.Offset = Offset
global.OffsetType = OffsetType
} else if (typeof (globalThis as { window?: Window }).window !== "undefined") {
const win = (globalThis as unknown as { window: Window }).window
win.AmqpManagement = AmqpManagement
win.createEnvironment = createEnvironment
win.AmqpEnvironment = AmqpEnvironment
win.AmqpConnection = AmqpConnection
win.AmqpPublisher = AmqpPublisher
win.AmqpConsumer = AmqpConsumer
win.createAmqpMessage = createAmqpMessage
win.OutcomeState = OutcomeState
win.Offset = Offset
win.OffsetType = OffsetType
}

export {
AmqpManagement,
AmqpEnvironment,
AmqpConnection,
AmqpPublisher,
AmqpConsumer,
createEnvironment,
createAmqpMessage,
OutcomeState,
Offset,
OffsetType,
}

export type { Management, Environment, Connection, Publisher, Consumer }
5 changes: 5 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare global {
const AmqpManagement: typeof import("./management.js").AmqpManagement
const createEnvironment: typeof import("./environment.js").createEnvironment
const AmqpEnvironment: typeof import("./environment.js").AmqpEnvironment
}
23 changes: 20 additions & 3 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
{
"extends": "@tsconfig/node22/tsconfig.json",
"compilerOptions": {
"preserveConstEnums": true,
"noEmit": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "Node",
"target": "ES2020",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noImplicitThis": true,
"strictNullChecks": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"preserveConstEnums": true,
"removeComments": false,
"sourceMap": true,
"outDir": "dist"
"declaration": true,
"declarationMap": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"allowJs": false,
"checkJs": false,
"types": ["node"],
"typeRoots": ["node_modules/@types"]
},
"include": ["src/**/*"],
"exclude": ["node_modules", "examples", "**/*.spec.ts", "**/*.test.ts"]
"exclude": ["node_modules", "dist", "examples", "test", "**/*.spec.ts", "**/*.test.ts", "vite.config.ts"]
}
19 changes: 0 additions & 19 deletions tsup.config.ts

This file was deleted.

40 changes: 40 additions & 0 deletions vite.config.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { defineConfig } from "vite"
import { resolve } from "path"
import { nodePolyfills } from "vite-plugin-node-polyfills"

export default defineConfig({
plugins: [
nodePolyfills({
globals: {
Buffer: true,
global: true,
process: true,
},
protocolImports: true,
}),
],
build: {
lib: {
entry: resolve(__dirname, "src/index_browser.ts"),
name: "RabbitmqAmqpClient",
formats: ["umd"],
fileName: () => `index.umd.js`,
},
rollupOptions: {
external: [],
output: {
globals: {},
},
},
outDir: "dist",
emptyOutDir: false,
sourcemap: true,
minify: false,
target: ["es2020"],
},
resolve: {
alias: {
"@": resolve(__dirname, "src"),
},
},
})
Loading