Skip to content

Commit

Permalink
lib: convert to esm
Browse files Browse the repository at this point in the history
  • Loading branch information
MylesBorins committed Apr 22, 2020
1 parent 3611e12 commit 5bd66dd
Show file tree
Hide file tree
Showing 26 changed files with 769 additions and 287 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ globals:
SharedArrayBuffer: readonly
parserOptions:
ecmaVersion: 2018
sourceType: module
rules:
quotes:
- error
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,10 @@ jobs:
- name: Install Dependencies
run: npm ci

- name: Test-Legacy
if: matrix.node-version == '10.x'
run: npm run test-legacy

- name: Test
run: npm run test-ci
if: matrix.node-version != '10.x'
run: npm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.nyc_output/
coverage/
dist/
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.nyc_output/
coverage/
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ Install using npm
npm install node-osc
```

## ⚠️ Experimental ⚠️

This is an experimental ESM version of node-osc make sure to run node with the `--experimental-modules` flag. This version require at minimum Node.js 13.2.0

## Example

### Sending OSC messages:

```js
const { Client } = require('node-osc');
import { Client } from 'node-osc';

const client = new Client('127.0.0.1', 3333);
client.send('/oscAddress', 200, () => {
Expand All @@ -24,7 +28,7 @@ client.send('/oscAddress', 200, () => {
### Listening for OSC messages:

```js
var { Server } = require('../lib');
import { Server } from 'node-osc';

var oscServer = new Server(3333, '0.0.0.0');

Expand All @@ -34,10 +38,12 @@ oscServer.on('message', function (msg) {
});
```

### Now with ESM!
### Legacy API

This just works due to conditional exports, isn't that cool!

```js
import { Client, Server } from 'node-osc';
const { Client, Server } = require('node-osc');

const client = new Client('127.0.0.1', 3333);
var server = new Server(3333, '0.0.0.0');
Expand Down
2 changes: 1 addition & 1 deletion examples/client.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */

'use strict';
const { Client, Message } = require('../lib');
const { Client, Message } = require('node-osc');

const client = new Client('127.0.0.1', 3333);

Expand Down
2 changes: 1 addition & 1 deletion examples/esm.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client, Server } from 'node-osc'
import { Client, Server } from 'node-osc';

const client = new Client('127.0.0.1', 3333);
var server = new Server(3333, '0.0.0.0');
Expand Down
2 changes: 1 addition & 1 deletion examples/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */

'use strict';
var { Server } = require('../lib');
var { Server } = require('node-osc');

var oscServer = new Server(3333, '0.0.0.0');

Expand Down
12 changes: 5 additions & 7 deletions lib/Client.js → lib/Client.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';
import { createSocket } from 'dgram';
import oscMin from 'osc-min';
import Message from './Message.mjs';

const { createSocket } = require('dgram');

const { toBuffer } = require('osc-min');

const Message = require('./Message');
const { toBuffer } = oscMin;

class Client {
constructor(host, port) {
Expand Down Expand Up @@ -64,4 +62,4 @@ class Client {
}
}

module.exports = Client;
export default Client;
14 changes: 4 additions & 10 deletions lib/Message.js → lib/Message.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

class Argument {
constructor(type, value) {
this.type = type;
Expand All @@ -11,12 +9,11 @@ class Message {
constructor(address, ...args) {
this.oscType = 'message';
this.address = address;
this.args = [];
args.forEach(arg => this.append(arg));
this.args = args;
}

append(arg) {
var argOut;
let argOut;
switch (typeof arg) {
case 'object':
if (arg instanceof Array) {
Expand All @@ -30,24 +27,21 @@ class Message {
case 'number':
if (Math.floor(arg) === arg) {
argOut = new Argument('integer', arg);
this.args.push(argOut);
} else {
argOut = new Argument('float', arg);
this.args.push(argOut);
}
break;
case 'string':
argOut = new Argument('string', arg);
this.args.push(argOut);
break;
case 'boolean':
argOut = new Argument('boolean', arg);
this.args.push(argOut);
break;
default:
throw new Error(`don't know how to encode ${arg}`);
}
if (argOut) this.args.push(argOut);
}
}

module.exports = Message;
export default Message;
11 changes: 4 additions & 7 deletions lib/Server.js → lib/Server.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/* eslint-disable no-console */
'use strict';
import { createSocket } from 'dgram';
import { EventEmitter } from 'events';

const { createSocket } = require('dgram');
const { EventEmitter } = require('events');

const decode = require('./decode');
import decode from './decode.mjs';

class Server extends EventEmitter {
constructor(port, host) {
Expand Down Expand Up @@ -38,4 +35,4 @@ class Server extends EventEmitter {
}
}

module.exports = Server;
export default Server;
8 changes: 3 additions & 5 deletions lib/decode.js → lib/decode.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const {
import {
TInt,
TFloat,
TString,
Expand All @@ -8,7 +7,7 @@ const {
// TTime,
TTrue,
TFalse
} = require('./types');
} from './types.mjs';

const tagToConstructor = {
i: TInt,
Expand All @@ -27,7 +26,6 @@ function decode(data) {
// we start getting the <address> and <rest> of OSC msg /<address>\0<rest>\0<typetags>\0<data>
const address = new TString();
data = address.decode(data);

if (data.length <= 0) {
return message;
}
Expand Down Expand Up @@ -57,4 +55,4 @@ function decode(data) {
return message;
}

module.exports = decode;
export default decode;
11 changes: 0 additions & 11 deletions lib/index.js

This file was deleted.

14 changes: 3 additions & 11 deletions lib/index.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import Message from './Message.js'
import Server from './Server.js'
import Client from './Client.js'

export { Client, Message, Server }

export default {
Client,
Message,
Server
}
export { default as Message } from './Message.mjs';
export { default as Server } from './Server.mjs';
export { default as Client } from './Client.mjs';
6 changes: 3 additions & 3 deletions lib/types.js → lib/types.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
import jspackMain from 'jspack';

const jspack = require('jspack').jspack;
const jspack = jspackMain.jspack;

class ShortBuffer {
constructor(type, buf, requiredLength) {
Expand Down Expand Up @@ -129,7 +129,7 @@ TTime.prototype = {
}
};

module.exports = {
export {
TString,
TInt,
TTime,
Expand Down
Loading

0 comments on commit 5bd66dd

Please sign in to comment.