Skip to content
Closed
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
13 changes: 8 additions & 5 deletions .github/workflows/test.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ name: Node.js CI

on:
push:
branches: [main]
branches:
- main
pull_request:
branches: [main]
branches:
- main

jobs:
build:
Expand All @@ -20,11 +22,11 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 16.x, 18.x, 20.x]
node-version: [14.x, 16.x, 18.x, 20.x, 22.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v5
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
Expand All @@ -48,4 +50,5 @@ jobs:
- run: echo 'HELO\n\n\n\n' | nc localhost 5672 | grep AMQP

# Run the tests
- run: make test
- run: npm run test
- run: npm run build --if-present
16 changes: 9 additions & 7 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: Publish

on:
release:
types: [created]
types:
- created

jobs:
build:
Expand All @@ -15,14 +16,14 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x, 16.x, 18.x, 20.x]
node-version: [14.x, 16.x, 18.x, 20.x, 22.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v5

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "npm"
Expand All @@ -41,18 +42,19 @@ jobs:

- run: echo 'HELO\n\n\n\n' | nc localhost 5672 | grep AMQP

- run: make test
- run: npm run test

publish:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v5
- uses: actions/setup-node@v4
with:
node-version: '20.x'
node-version: '22.x'
cache: "npm"
registry-url: https://registry.npmjs.org/

- run: npm ci
- run: npm publish --dry-run
env:
Expand Down
14 changes: 9 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
*~
scratch
node_modules/
etc/
coverage/
/.idea/
.nyc_output/
node_modules
etc
coverage
.idea
.nyc_output
dist

.env*
!.env.example
114 changes: 114 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { readdir, stat, rm, mkdir, readFile, writeFile } from 'node:fs/promises'
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { transform } from '@swc/core'

async function* scan(pathname) {
const entries = await readdir(pathname)
for (const entry of entries) {
const stats = await stat(join(pathname, entry))
if (stats.isDirectory()) {
yield* scan(join(pathname, entry))
continue
}

yield join(pathname, entry)
}
}

async function bootstrap() {
const sourceRoot = 'lib'
const outDir = 'dist'

await rm(join(fileURLToPath(import.meta.url), '..', outDir), {
recursive: true,
force: true
})

for await (const entry of scan(sourceRoot)) {
const source = await readFile(entry, 'utf-8')

{
const { code } = await transform(source, {
jsc: {
baseUrl: join(fileURLToPath(import.meta.url), '..'),
parser: {
syntax: 'ecmascript'
},

target: 'es2020',
keepClassNames: true,

paths: {
'#/*': ['./src/*']
}
},
module: {
type: 'es6',
strict: true,
importInterop: 'swc'
}
})

const dir = dirname(entry)

await mkdir(dir.replace(sourceRoot, join(outDir, 'esm')), {
recursive: true
})

await writeFile(
join(
fileURLToPath(import.meta.url),
'..',
entry.replace(sourceRoot, join(outDir, 'esm'))
),
code
)
}

{
const { code } = await transform(source, {
jsc: {
baseUrl: join(fileURLToPath(import.meta.url), '..'),
parser: {
syntax: 'ecmascript'
},

target: 'es2020',
keepClassNames: true,

paths: {
'#/*': ['./src/*']
}
},
module: {
type: 'commonjs',
strict: true,
importInterop: 'swc'
}
})

const dir = dirname(entry)

await mkdir(dir.replace(sourceRoot, join(outDir, 'cjs')), {
recursive: true
})

await writeFile(
join(dir.replace(sourceRoot, join(outDir, 'cjs')), 'package.json'),
JSON.stringify({ type: 'commonjs' }, null, 2)
)

await writeFile(
join(
fileURLToPath(import.meta.url),
'..',
entry.replace(sourceRoot, join(outDir, 'cjs'))
),
code
)
}
}
}

bootstrap()
22 changes: 0 additions & 22 deletions callback_api.js

This file was deleted.

16 changes: 0 additions & 16 deletions channel_api.js

This file was deleted.

45 changes: 45 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { promisify } from 'node:util'

import { connect as raw_connect } from './connect.js'
import { ChannelModel } from './channel_model.js'
import { CallbackModel } from './callback_model.js'

export * as credentials from './credentials.js'
import * as credentials from './credentials.js'

export { IllegalOperationError } from './error.js'
import { IllegalOperationError } from './error.js'

export function connect(url, options, cb) {
// Handle different argument patterns
if (typeof url === 'function') {
cb = url
url = undefined
options = undefined
} else if (typeof options === 'function') {
cb = options
options = undefined
}

// If callback is provided, use callback style
if (typeof cb === 'function') {
return raw_connect(url, options, function (err, conn) {
if (err === null) {
cb(null, new CallbackModel(conn))
} else {
cb(err)
}
})
}

// Otherwise return a promise
return promisify((cb) => raw_connect(url, options, cb))().then(
(conn) => new ChannelModel(conn)
)
}

export default {
connect,
credentials,
IllegalOperationError
}
4 changes: 1 addition & 3 deletions lib/api_args.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
//
//

'use strict';

/*
The channel (promise) and callback APIs have similar signatures, and
in particular, both need AMQP fields prepared from the same arguments
Expand Down Expand Up @@ -311,4 +309,4 @@ Args.recover = function() {
return {requeue: true};
};

module.exports = Object.freeze(Args);
export default Object.freeze(Args);
5 changes: 1 addition & 4 deletions lib/bitset.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
//
//

'use strict';

/**
* A bitset implementation, after that in java.util. Yes there
* already exist such things, but none implement next{Clear|Set}Bit or
* equivalent, and none involved me tooling about for an evening.
*/
class BitSet {
export class BitSet {
/**
* @param {number} [size]
*/
Expand Down Expand Up @@ -126,5 +125,3 @@ function trailingZeros(i) {
y = i << 2; if (y != 0) { n = n - 2; i = y; }
return n - ((i << 1) >>> 31);
}

module.exports.BitSet = BitSet;
19 changes: 7 additions & 12 deletions lib/callback_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
//
//

'use strict';
import { EventEmitter } from 'node:events'

var defs = require('./defs');
var EventEmitter = require('events');
var BaseChannel = require('./channel').BaseChannel;
var acceptMessage = require('./channel').acceptMessage;
var Args = require('./api_args');
import * as defs from './defs.js';
import { BaseChannel, acceptMessage } from './channel.js'
import Args from './api_args.js'

class CallbackModel extends EventEmitter {
export class CallbackModel extends EventEmitter {
constructor (connection) {
super();
this.connection = connection;
Expand Down Expand Up @@ -68,7 +66,7 @@ class CallbackModel extends EventEmitter {
}
}

class Channel extends BaseChannel {
export class Channel extends BaseChannel {
constructor (connection) {
super(connection);
this.on('delivery', this.handleDelivery.bind(this));
Expand Down Expand Up @@ -299,7 +297,7 @@ function callbackWrapper(ch, cb) {
} : function() {};
}

class ConfirmChannel extends Channel {
export class ConfirmChannel extends Channel {
publish (exchange, routingKey,
content, options, cb) {
this.pushConfirmCallback(cb);
Expand Down Expand Up @@ -337,6 +335,3 @@ class ConfirmChannel extends Channel {
}
}

module.exports.CallbackModel = CallbackModel;
module.exports.Channel = Channel;
module.exports.ConfirmChannel = ConfirmChannel;
Loading