Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix all the things for modern node ✅ #1

Merged
merged 8 commits into from
Feb 20, 2025
Merged
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
9 changes: 5 additions & 4 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Used for Jest
{
"presets": [
"@babel/preset-env"
],
"plugins": [
[ "@babel/plugin-proposal-decorators", { "decoratorsBeforeExport": true, "legacy": false } ],
"@babel/plugin-proposal-class-properties",
"@babel/plugin-syntax-bigint",
"@babel/plugin-transform-async-to-generator"
["@babel/plugin-proposal-decorators", { "version": "2023-11" }]
]
}
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"env": {
"node": true,
"mocha": true,
"es6": true
"es6": true,
"jest": true
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ npm-debug.log

*.swp
*.swo

.idea/
142 changes: 71 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Desktop users can use this library to create their own scripts and utilities to
As of now, dbus-next targets the latest features of JavaScript. The earliest version supported is `6.3.0`. However, the library uses `BigInt` by default for the long integer types which was introduced in `10.8.0`. If you need to support versions earlier than this, set BigInt compatibility mode. This will configure the library to use [JSBI](https://github.com/GoogleChromeLabs/jsbi) as a polyfill for long types.

```javascript
const dbus = require('dbus-next');
const dbus = require('@particle/dbus-next');
dbus.setBigIntCompat(true);
```

Expand All @@ -36,7 +36,7 @@ The interface object is an event emitter that will emit the name of a signal whe
This is a brief example of using a proxy object with the [MPRIS](https://specifications.freedesktop.org/mpris-spec/latest/Player_Interface.html) media player interface.

```js
let dbus = require('dbus-next');
let dbus = require('@particle/dbus-next');
let bus = dbus.sessionBus();
let Variant = dbus.Variant;

Expand Down Expand Up @@ -68,79 +68,79 @@ properties.on('PropertiesChanged', (iface, changed, invalidated) => {
For a complete example, see the [MPRIS client](https://github.com/dbusjs/node-dbus-next/blob/master/examples/mpris.js) example which can be used to control media players on the command line.

## The Service Interface

You can use the `Interface` class to define your interfaces. This interfaces uses the proposed [decorators syntax](https://github.com/tc39/proposal-decorators) which is not yet part of the ECMAScript standard, but should be included one day. Unfortunately, you'll need a [Babel plugin](https://www.npmjs.com/package/@babel/plugin-proposal-decorators) to make this code work for now.
You can use the `Interface` class to define your interfaces.

```js
let dbus = require('dbus-next');
let Variant = dbus.Variant;
import dbus, { Variant, DBusError } from '@particle/dbus-next';

let {
Interface, property, method, signal, DBusError,
ACCESS_READ, ACCESS_WRITE, ACCESS_READWRITE
const {
Interface, property, method, signal,
ACCESS_READ, ACCESS_WRITE, ACCESS_READWRITE
} = dbus.interface;

let bus = dbus.sessionBus();

class ExampleInterface extends Interface {
@property({signature: 's', access: ACCESS_READWRITE})
SimpleProperty = 'foo';

_MapProperty = {
'foo': new Variant('s', 'bar'),
'bat': new Variant('i', 53)
};

@property({signature: 'a{sv}'})
get MapProperty() {
return this._MapProperty;
}

set MapProperty(value) {
this._MapProperty = value;

Interface.emitPropertiesChanged(this, {
MapProperty: value
});
}

@method({inSignature: 's', outSignature: 's'})
Echo(what) {
return what;
}

@method({inSignature: 'ss', outSignature: 'vv'})
ReturnsMultiple(what, what2) {
return [
new Variant('s', what),
new Variant('s', what2)
];
}

@method({inSignature: '', outSignature: ''})
ThrowsError() {
// the error is returned to the client
throw new DBusError('org.test.iface.Error', 'something went wrong');
}

@method({inSignature: '', outSignature: '', noReply: true})
NoReply() {
// by setting noReply to true, dbus-next will NOT send a return reply through dbus
// after the method is called.
}

@signal({signature: 's'})
HelloWorld(value) {
return value;
}

@signal({signature: 'ss'})
SignalMultiple(x) {
return [
'hello',
'world'
];
}
export class ExampleInterface extends Interface {
// Can be changed internally or from the outside, but must manually emit properties changed like in set MapProperty
@property({ signature: 's', access: ACCESS_READWRITE })
SimpleProperty = 'foo';

_MapProperty: Record<string, Variant> = {
'foo': new Variant('s', 'bar'),
'bat': new Variant('i', 53)
};

@property({ signature: 'a{sv}' })
get MapProperty(): Record<string, Variant> {
return this._MapProperty;
}

set MapProperty(value: Record<string, Variant>) {
this._MapProperty = value;

Interface.emitPropertiesChanged(
this,
{ MapProperty: value },
[]
);
}

@method({ inSignature: 's', outSignature: 's' })
Echo(what: string): string {
return what;
}

@method({ inSignature: 'ss', outSignature: 'vv' })
ReturnsMultiple(what: string, what2: string) {
return [
new Variant('s', what),
new Variant('s', what2)
];
}

@method({ inSignature: '', outSignature: '' })
ThrowsError(): void {
// the error is returned to the client
throw new DBusError('org.test.iface.Error', 'something went wrong');
}

@method({ inSignature: '', outSignature: '', noReply: true })
NoReply(): void {
// by setting noReply to true, dbus-next will NOT send a return reply through dbus
// after the method is called.
}

@signal({ signature: 's' })
HelloWorld(value: string): string {
// Transform value before emit
return value;
}

@signal({ signature: 'ss' })
SignalMultiple(x) {
return [
'hello',
'world'
];
}
}

let example = new ExampleInterface('org.test.iface');
Expand Down Expand Up @@ -176,7 +176,7 @@ If you have an interface xml description, which can be gotten from the `org.free
The low-level interface can be used to interact with messages directly. Create new messages with the `Message` class to be sent on the bus as method calls, signals, method returns, or errors. Method calls can be called with the `call()` method of the `MessageBus` to await a reply and `send()` can be use for messages that don't expect a reply.

```js
let dbus = require('dbus-next');
let dbus = require('@particle/dbus-next');
let Message = dbus.Message;

let bus = dbus.sessionBus();
Expand Down
1 change: 1 addition & 0 deletions bin/dbus-next-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// TODO: test signal sending

const program = require('commander');
/** @type {import('@particle/dbus-next')} */
const dbus = require('../');
const Message = dbus.Message;
const {
Expand Down
3 changes: 2 additions & 1 deletion bin/generate-client-interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const xml2js = require('xml2js');
const Handlebars = require('handlebars');
let parser = new xml2js.Parser();
const program = require('commander');
/** @type {import('@particle/dbus-next')} */
const dbus = require('../');
const Message = dbus.Message;
const {
Expand Down Expand Up @@ -351,7 +352,7 @@ async function main() {
ifs.result = template({
interfaces: [ifs.interface],
xmlData: ifs.objectPaths.length === 1 ? ifs.xmlData : undefined,
objectPath: objectPath,
objectPath: objectPath,
serviceName: ifs.serviceName
});

Expand Down
2 changes: 1 addition & 1 deletion bin/generate-interfaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ program
.parse(process.argv);

const templateData = `
let dbus = require('dbus-next');
let dbus = require('@particle/dbus-next');
let Variant = dbus.Variant;

let {
Expand Down
9 changes: 5 additions & 4 deletions examples/mpris.js → examples/mpris.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node
let dbus = require('../');
let program = require('commander');
// @ts-nocheck
import dbus from "@particle/dbus-next";
import program from "commander";

const MPRIS_IFACE = 'org.mpris.MediaPlayer2.Player';
const MPRIS_PATH = '/org/mpris/MediaPlayer2';
Expand All @@ -12,7 +13,7 @@ async function listAll() {
let iface = obj.getInterface('org.freedesktop.DBus');
let names = await iface.ListNames();
let result = names.filter((n) => n.startsWith('org.mpris.MediaPlayer2'))

return result;
}

Expand Down Expand Up @@ -45,7 +46,7 @@ async function printNames() {
}

function printMetadata(metadata) {
for (k of Object.keys(metadata.value)) {
for (const k of Object.keys(metadata.value)) {
let value = metadata.value[k].value;
console.log(k.padEnd(23) + value);
}
Expand Down
7 changes: 0 additions & 7 deletions examples/service/.babelrc

This file was deleted.

93 changes: 0 additions & 93 deletions examples/service/index.js

This file was deleted.

Loading