Skip to content
Permalink
Browse files

Refactor to make plugins in consuming modules easier to type.

  • Loading branch information...
legastero committed Apr 25, 2019
1 parent 33bdec3 commit c0c5d0c8f8fdf5b10c7631d0e945d14d761cb034
Showing with 721 additions and 826 deletions.
  1. +42 −24 docs/Create_Plugin.md
  2. +1 −1 package.json
  3. +3 −3 src/Client.ts
  4. +1 −1 src/Definitions.ts
  5. +2 −8 src/DiscoManager.ts
  6. +1 −1 src/{protocol → }/JID.ts
  7. +1 −1 src/StreamManagement.ts
  8. +1 −1 src/{protocol → helpers}/DataForms.ts
  9. +1 −1 src/{protocol → helpers}/LegacyEntityCapabilities.ts
  10. +6 −6 src/index.ts
  11. +1 −1 src/jingle/FileTransferSession.ts
  12. +1 −1 src/jingle/ICESession.ts
  13. +1 −1 src/jingle/MediaSession.ts
  14. +1 −1 src/jingle/Session.ts
  15. +1 −1 src/jingle/SessionManager.ts
  16. +1 −1 src/jingle/lib/Protocol.ts
  17. +1 −1 src/plugins/attention.ts
  18. +1 −1 src/plugins/avatar.ts
  19. +1 −1 src/plugins/blocking.ts
  20. +1 −1 src/plugins/bob.ts
  21. +2 −2 src/plugins/bookmarks.ts
  22. +2 −2 src/plugins/carbons.ts
  23. +1 −1 src/plugins/chatstates.ts
  24. +1 −1 src/plugins/command.ts
  25. +1 −1 src/plugins/correction.ts
  26. +1 −1 src/plugins/csi.ts
  27. +1 −1 src/plugins/dataforms.ts
  28. +2 −2 src/plugins/disco.ts
  29. +1 −2 src/plugins/extdisco.ts
  30. +1 −1 src/plugins/features.ts
  31. +1 −1 src/plugins/geoloc.ts
  32. +1 −1 src/plugins/invisible.ts
  33. +1 −1 src/plugins/jingle.ts
  34. +3 −3 src/plugins/mam.ts
  35. +2 −2 src/plugins/markers.ts
  36. +1 −1 src/plugins/mood.ts
  37. +2 −2 src/plugins/muc.ts
  38. +1 −1 src/plugins/nick.ts
  39. +1 −1 src/plugins/ping.ts
  40. +1 −1 src/plugins/private.ts
  41. +1 −1 src/plugins/pubsub.ts
  42. +1 −1 src/plugins/push.ts
  43. +1 −1 src/plugins/reach.ts
  44. +1 −1 src/plugins/receipts.ts
  45. +1 −1 src/plugins/register.ts
  46. +2 −2 src/plugins/roster.ts
  47. +1 −1 src/plugins/rtt.ts
  48. +1 −1 src/plugins/sasl.ts
  49. +1 −1 src/plugins/smacks.ts
  50. +1 −1 src/plugins/time.ts
  51. +1 −1 src/plugins/tune.ts
  52. +2 −3 src/plugins/upload.ts
  53. +1 −1 src/plugins/vcard.ts
  54. +1 −1 src/plugins/version.ts
  55. +227 −6 src/protocol/index.ts
  56. +3 −4 src/protocol/{stanzas → }/rfc3921.ts
  57. +115 −104 src/protocol/{stanzas → }/rfc6120.ts
  58. +4 −4 src/protocol/{stanzas → }/rfc6121.ts
  59. +3 −4 src/protocol/{stanzas → }/rfc7395.ts
  60. +0 −307 src/protocol/stanzas/index.ts
  61. +2 −2 src/protocol/{stanzas → }/util.ts
  62. +3 −4 src/protocol/{stanzas → }/xep0004.ts
  63. +3 −4 src/protocol/{stanzas → }/xep0016.ts
  64. +3 −4 src/protocol/{stanzas → }/xep0030.ts
  65. +3 −4 src/protocol/{stanzas → }/xep0033.ts
  66. +3 −4 src/protocol/{stanzas → }/xep0045.ts
  67. +3 −4 src/protocol/{stanzas → }/xep0047.ts
  68. +3 −5 src/protocol/{stanzas → }/xep0048.ts
  69. +3 −4 src/protocol/{stanzas → }/xep0049.ts
  70. +3 −4 src/protocol/{stanzas → }/xep0050.ts
  71. +3 −4 src/protocol/{stanzas → }/xep0054.ts
  72. +3 −4 src/protocol/{stanzas → }/xep0055.ts
  73. +2 −2 src/protocol/{stanzas → }/xep0059.ts
  74. +3 −4 src/protocol/{stanzas → }/xep0060.ts
  75. +3 −10 src/protocol/{stanzas → }/xep0065.ts
  76. +3 −4 src/protocol/{stanzas → }/xep0066.ts
  77. +3 −4 src/protocol/{stanzas → }/xep0071.ts
  78. +3 −4 src/protocol/{stanzas → }/xep0077.ts
  79. +2 −3 src/protocol/{stanzas → }/xep0080.ts
  80. +2 −3 src/protocol/{stanzas → }/xep0084.ts
  81. +4 −4 src/protocol/{stanzas → }/xep0085.ts
  82. +3 −4 src/protocol/{stanzas → }/xep0092.ts
  83. +4 −4 src/protocol/{stanzas → }/xep0107.ts
  84. +3 −3 src/protocol/{stanzas → }/xep0108.ts
  85. +2 −2 src/protocol/{stanzas → }/xep0114.ts
  86. +3 −4 src/protocol/{stanzas → }/xep0115.ts
  87. +3 −3 src/protocol/{stanzas → }/xep0118.ts
  88. +2 −3 src/protocol/{stanzas → }/xep0122.ts
  89. +3 −3 src/protocol/{stanzas → }/xep0124.ts
  90. +4 −4 src/protocol/{stanzas → }/xep0131.ts
  91. +3 −4 src/protocol/{stanzas → }/xep0138.ts
  92. +2 −3 src/protocol/{stanzas → }/xep0141.ts
  93. +4 −4 src/protocol/{stanzas → }/xep0144.ts
  94. +3 −3 src/protocol/{stanzas → }/xep0152.ts
  95. +3 −3 src/protocol/{stanzas → }/xep0153.ts
  96. +3 −3 src/protocol/{stanzas → }/xep0158.ts
  97. +3 −3 src/protocol/{stanzas → }/xep0166.ts
  98. +2 −2 src/protocol/{stanzas → }/xep0167.ts
  99. +4 −4 src/protocol/{stanzas → }/xep0172.ts
  100. +2 −2 src/protocol/{stanzas → }/xep0176.ts
  101. +2 −2 src/protocol/{stanzas → }/xep0177.ts
  102. +3 −4 src/protocol/{stanzas → }/xep0184.ts
  103. +3 −4 src/protocol/{stanzas → }/xep0186.ts
  104. +4 −4 src/protocol/{stanzas → }/xep0191.ts
  105. +4 −4 src/protocol/{stanzas → }/xep0198.ts
  106. +4 −4 src/protocol/{stanzas → }/xep0199.ts
  107. +3 −4 src/protocol/{stanzas → }/xep0202.ts
  108. +4 −4 src/protocol/{stanzas → }/xep0203.ts
  109. +25 −25 src/protocol/{stanzas → }/xep0215.ts
  110. +2 −3 src/protocol/{stanzas → }/xep0221.ts
  111. +4 −4 src/protocol/{stanzas → }/xep0224.ts
  112. +3 −4 src/protocol/{stanzas → }/xep0231.ts
  113. +2 −2 src/protocol/{stanzas → }/xep0234.ts
  114. +2 −2 src/protocol/{stanzas → }/xep0247.ts
  115. +2 −2 src/protocol/{stanzas → }/xep0260.ts
  116. +3 −3 src/protocol/{stanzas → }/xep0261.ts
  117. +3 −4 src/protocol/{stanzas → }/xep0264.ts
  118. +4 −4 src/protocol/{stanzas → }/xep0280.ts
  119. +5 −4 src/protocol/{stanzas → }/xep0297.ts
  120. +2 −2 src/protocol/{stanzas → }/xep0300.ts
  121. +3 −4 src/protocol/{stanzas → }/xep0301.ts
  122. +4 −4 src/protocol/{stanzas → }/xep0308.ts
  123. +4 −4 src/protocol/{stanzas → }/xep0313.ts
  124. +4 −4 src/protocol/{stanzas → }/xep0319.ts
  125. +2 −2 src/protocol/{stanzas → }/xep0320.ts
  126. +3 −4 src/protocol/{stanzas → }/xep0333.ts
  127. +4 −4 src/protocol/{stanzas → }/xep0334.ts
  128. +4 −4 src/protocol/{stanzas → }/xep0335.ts
  129. +2 −3 src/protocol/{stanzas → }/xep0338.ts
  130. +2 −2 src/protocol/{stanzas → }/xep0343.ts
  131. +2 −2 src/protocol/{stanzas → }/xep0352.ts
  132. +4 −4 src/protocol/{stanzas → }/xep0357.ts
  133. +4 −4 src/protocol/{stanzas → }/xep0359.ts
  134. +4 −4 src/protocol/{stanzas → }/xep0363.ts
  135. +3 −4 src/protocol/{stanzas → }/xep0380.ts
  136. +2 −2 src/protocol/{stanzas → }/xrd.ts
  137. +1 −1 src/transports/bosh.ts
  138. +1 −1 src/transports/websocket.ts
  139. +1 −1 test/hostmeta.js
  140. +1 −1 test/jxt-definitions/index.ts
  141. +2 −1 tsconfig.json
@@ -1,49 +1,67 @@
# Creating a Stanza.io Plugin
# Creating a StanzaJS Plugin

```javascript
module.exports = function(client, stanzas) {
// 1. Create and register our custom `mystanza` stanza type
import { Agent, jxt } from 'stanza';
var types = stanzas.utils;
var Foo = stanzas.define({
name: 'mystanza',
// 1. Declare our new custom stanza type
export interface MyStanza {
type: string;
value: string;
}
// 2. Declare a new method for the StanzaJS agent
declare module 'stanza' {
export interface Agent {
sendMyStanza(jid: string, data: string): void;
}
}
// 3. Attach our new definition to message stanzas
declare module 'stanza/protocol' {
export interface Message {
mystanza?: MyStanza;
}
}
// 4. Create a plugin function
export default function (client: Agent, stanzas: jxt.Registry) {
// 5. Create and register our custom `mystanza` stanza definition
stanzas.define({
element: 'foo',
namespace: 'http://example.com/p/foo',
fields: {
type: types.attribute('type'),
value: types.text()
}
});
stanzas.withMessage(function(Message) {
stanzas.extend(Message, Foo);
type: jxt.attribute('type'),
value: jxt.text()
},
namespace: 'http://example.com/p/foo',
path: 'message.mystanza'
});
// 2. Add API to the Stanza client for sending `mystanza` data
client.sendMyStanza = function(jid, foo) {
// 6. Add API to the StanzaJS agent for sending `mystanza` data
client.sendMyStanza = (jid: string, data: string): void {
client.sendMessage({
to: jid,
mystanza: {
type: 'bar',
value: foo
value: data
}
});
};
// 3. Listen for incoming `mystanza` data and emit our own event
client.on('message', function(msg) {
// 7. Listen for incoming `mystanza` data and emit our own event
client.on('message', (msg: Message) => {
if (msg.mystanza) {
client.emit('foo', msg);
client.emit('mystanza', msg);
}
});
};
```

```javascript
// 4. Load our plugin
// 8. Load our plugin
import MyStanzaPlugin from 'path/to/plugin';
client.use(require('path/to/plugin'));
client.use(MyStanzaPlugin);
```
@@ -104,7 +104,7 @@
"compile:rollup-react-native": "rollup -c rollup-react-native.config.js",
"compile:webpack": "webpack --mode production",
"lint": "tslint -p .",
"test": "nyc ts-node --files test/index.js | tap-spec && nyc report --reporter=text",
"test": "nyc ts-node -O \"{ \\\"allowJs\\\": true }\" --files test/index.js | tap-spec && nyc report --reporter=text",
"validate": "npm ls"
}
}
@@ -1,6 +1,7 @@
import WildEmitter from './lib/WildEmitter';

import { Agent, AgentConfig, Transport } from './Definitions';
import * as JID from './JID';
import * as JXT from './jxt';
import * as SASL from './lib/sasl';
import Bind from './plugins/bind';
@@ -9,9 +10,8 @@ import HostMeta from './plugins/hostmeta';
import SASLPlugin from './plugins/sasl';
import Session from './plugins/session';
import Smacks from './plugins/smacks';
import * as JID from './protocol/JID';
import { IQ, Message, Presence, StreamError } from './protocol/stanzas';
import Protocol from './protocol/stanzas';
import { IQ, Message, Presence, StreamError } from './protocol';
import Protocol from './protocol';
import StreamManagement from './StreamManagement';
import BOSH from './transports/bosh';
import WebSocket from './transports/websocket';
@@ -12,7 +12,7 @@ import {
StreamError,
StreamFeatures,
StreamManagement
} from './protocol/stanzas';
} from './protocol';
import SM from './StreamManagement';

export interface TopLevelElements {
@@ -1,11 +1,5 @@
import * as EntityCaps from './protocol/LegacyEntityCapabilities';
import {
DataForm,
DiscoInfo,
DiscoInfoIdentity,
DiscoItem,
LegacyEntityCaps
} from './protocol/stanzas';
import * as EntityCaps from './helpers/LegacyEntityCapabilities';
import { DataForm, DiscoInfo, DiscoInfoIdentity, DiscoItem, LegacyEntityCaps } from './protocol';

export interface DiscoNodeInfo {
features: string[];
@@ -1,5 +1,5 @@
import punycode from 'punycode';
import { nameprep, nodeprep, resourceprep } from '../lib/stringprep';
import { nameprep, nodeprep, resourceprep } from './lib/stringprep';

export interface JIDParts {
domain: string;
@@ -7,7 +7,7 @@ import {
StreamManagementEnabled,
StreamManagementFailed,
StreamManagementResume
} from './protocol/stanzas';
} from './protocol';

const MAX_SEQ = Math.pow(2, 32);
const mod = (v: number, n: number) => ((v % n) + n) % n;
@@ -1,4 +1,4 @@
import { DataFormField } from './stanzas';
import { DataFormField } from '../protocol';

export function mergeFields(original: DataFormField[], updated: DataFormField[]): DataFormField[] {
const merged: DataFormField[] = [];
@@ -1,7 +1,7 @@
import * as Hashes from '../lib/crypto';
import { octetCompare } from '../Utils';

import { DataForm, DataFormField, DiscoInfo, DiscoInfoIdentity } from './stanzas';
import { DataForm, DataFormField, DiscoInfo, DiscoInfoIdentity } from '../protocol';

function escape(value: string): Buffer {
return new Buffer(value.replace(/</g, '&lt;'), 'utf-8');
@@ -1,17 +1,17 @@
import * as JID from './protocol/JID';

import Client from './Client';
import { AgentConfig } from './Definitions';
import { Agent, AgentConfig } from './Definitions';
import * as JID from './JID';
import * as JXT from './jxt';
import Plugins from './plugins';
import * as Stanzas from './protocol';

export const VERSION = '__STANZAJS_VERSION__';

export { Client, JXT, JID };
export { Agent, Client, JXT, JID, Stanzas };

export function createClient(opts: AgentConfig) {
export function createClient(opts: AgentConfig): Client & Agent {
const client = new Client(opts);
client.use(Plugins);

return client;
return client as Client & Agent;
}
@@ -9,7 +9,7 @@ import {
INFO_CHECKSUM_5,
Jingle,
NS_FILE_TRANSFER_5
} from '../protocol/stanzas';
} from '../protocol';
import ICESession from './ICESession';
import { exportToSDP, importFromSDP } from './lib/Intermediate';
import { Action, SessionRole } from './lib/JingleUtil';
@@ -1,7 +1,7 @@
import * as SDPUtils from './lib/SDP';

import { NS_JINGLE_ICE_UDP_1 } from '../protocol';
import { Jingle, JingleContent, JingleIceUdp, JingleReason } from '../protocol/stanzas';
import { Jingle, JingleContent, JingleIceUdp, JingleReason } from '../protocol';
import { exportToSDP, importFromSDP } from './lib/Intermediate';
import { Action, ReasonCondition, SessionRole } from './lib/JingleUtil';
import {
@@ -10,7 +10,7 @@ import {
JingleInfo,
JingleReason,
JingleRtpDescription
} from '../protocol/stanzas';
} from '../protocol';
import ICESession from './ICESession';
import { exportToSDP, importFromSDP } from './lib/Intermediate';
import { Action, ReasonCondition, SessionRole } from './lib/JingleUtil';
@@ -2,7 +2,7 @@ import { priorityQueue } from '../lib/async';
import { uuid } from '../Utils';

import WildEmitter from '../lib/WildEmitter';
import { Jingle, JingleReason } from '../protocol/stanzas';
import { Jingle, JingleReason } from '../protocol';
import { Action, ReasonCondition, SessionRole } from './lib/JingleUtil';
import SessionManager from './SessionManager';

@@ -3,7 +3,7 @@ import { octetCompare } from '../Utils';

import { NS_JINGLE_RTP_1 } from '../protocol';

import { IQ, Jingle, JingleReason, NS_FILE_TRANSFER_5, StanzaError } from '../protocol/stanzas';
import { IQ, Jingle, JingleReason, NS_FILE_TRANSFER_5, StanzaError } from '../protocol';
import FileTransferSession from './FileTransferSession';
import { Action, ReasonCondition } from './lib/JingleUtil';
import MediaSession from './MediaSession';
@@ -18,7 +18,7 @@ import {
JingleIceUdpCandidate,
JingleRtpCodec,
JingleRtpDescription
} from '../../protocol/stanzas';
} from '../../protocol';

export function convertIntermediateToApplication(
media: IntermediateMediaDescription,
@@ -1,6 +1,6 @@
import { Agent } from '../Definitions';
import { NS_ATTENTION_0 } from '../protocol';
import { Message } from '../protocol/stanzas';
import { Message } from '../protocol';

declare module '../Definitions' {
export interface Agent {
@@ -1,6 +1,6 @@
import { Agent } from '../Definitions';
import { NS_AVATAR_DATA, NS_AVATAR_METADATA, NS_PEP_NOTIFY } from '../protocol';
import { AvatarMetaData, IQ, Message, Presence } from '../protocol/stanzas';
import { AvatarMetaData, IQ, Message, Presence } from '../protocol';

declare module '../Definitions' {
export interface Agent {
@@ -1,5 +1,5 @@
import { Agent } from '../Definitions';
import { IQ } from '../protocol/stanzas';
import { IQ } from '../protocol';

declare module '../Definitions' {
export interface Agent {
@@ -1,6 +1,6 @@
import { Agent } from '../Definitions';
import { NS_BOB } from '../protocol';
import { IQ } from '../protocol/stanzas';
import { IQ } from '../protocol';

declare module '../Definitions' {
export interface Agent {
@@ -1,6 +1,6 @@
import { Agent } from '../Definitions';
import * as JID from '../protocol/JID';
import { BookmarkStorage, IQ, MUCBookmark } from '../protocol/stanzas';
import * as JID from '../JID';
import { BookmarkStorage, IQ, MUCBookmark } from '../protocol';

declare module '../Definitions' {
export interface Agent {
@@ -1,6 +1,6 @@
import { Agent } from '../Definitions';
import * as JID from '../protocol/JID';
import { IQ, Message } from '../protocol/stanzas';
import * as JID from '../JID';
import { IQ, Message } from '../protocol';

declare module '../Definitions' {
export interface Agent {
@@ -1,6 +1,6 @@
import { Agent } from '../Definitions';
import { NS_CHAT_STATES } from '../protocol';
import { Message } from '../protocol/stanzas';
import { Message } from '../protocol';

const ALLOWED_TYPES = ['chat', 'groupcaht', 'normal'];

@@ -1,6 +1,6 @@
import { Agent } from '../Definitions';
import { NS_ADHOC_COMMANDS } from '../protocol';
import { IQ } from '../protocol/stanzas';
import { IQ } from '../protocol';

declare module '../Definitions' {
export interface Agent {
@@ -1,6 +1,6 @@
import { Agent } from '../Definitions';
import { NS_CORRECTION_0 } from '../protocol';
import { Message } from '../protocol/stanzas';
import { Message } from '../protocol';

export default function(client: Agent) {
client.disco.addFeature(NS_CORRECTION_0);
@@ -1,5 +1,5 @@
import { Agent } from '../Definitions';
import { CSI } from '../protocol/stanzas';
import { CSI } from '../protocol';

declare module '../Definitions' {
export interface Agent {
@@ -5,7 +5,7 @@ import {
NS_DATAFORM_MEDIA,
NS_DATAFORM_VALIDATION
} from '../protocol';
import { Message } from '../protocol/stanzas';
import { Message } from '../protocol';

export default function(client: Agent) {
client.disco.addFeature(NS_DATAFORM);
@@ -1,8 +1,8 @@
import { Agent } from '../Definitions';
import Disco, { DiscoNodeInfo } from '../DiscoManager';
import * as JID from '../JID';
import { NS_DISCO_INFO, NS_DISCO_ITEMS } from '../protocol';
import * as JID from '../protocol/JID';
import { DiscoInfo, DiscoItems, IQ, LegacyEntityCaps, Presence } from '../protocol/stanzas';
import { DiscoInfo, DiscoItems, IQ, LegacyEntityCaps, Presence } from '../protocol';

declare module '../Definitions' {
export interface Agent {
@@ -1,6 +1,5 @@
import { Agent } from '../Definitions';
import { IQ } from '../protocol/stanzas';
import { ExternalServiceCredentials, ExternalServiceList } from '../protocol/stanzas/xep0215';
import { ExternalServiceCredentials, ExternalServiceList, IQ } from '../protocol';

declare module '../Definitions' {
export interface Agent {
@@ -1,5 +1,5 @@
import { Agent } from '../Definitions';
import { StreamFeatures } from '../protocol/stanzas';
import { StreamFeatures } from '../protocol';

declare module '../Definitions' {
export interface Agent {
@@ -1,6 +1,6 @@
import { Agent } from '../Definitions';
import { NS_GEOLOC, NS_PEP_NOTIFY } from '../protocol';
import { Geolocation, IQ, Message } from '../protocol/stanzas';
import { Geolocation, IQ, Message } from '../protocol';

declare module '../Definitions' {
export interface Agent {
@@ -1,5 +1,5 @@
import { Agent } from '../Definitions';
import { IQ } from '../protocol/stanzas';
import { IQ } from '../protocol';

declare module '../Definitions' {
export interface Agent {
@@ -13,7 +13,7 @@ import {
NS_JINGLE_RTP_SSMA_0,
NS_JINGLE_RTP_VIDEO
} from '../protocol';
import { IQ, Jingle as JingleRequest, NS_JINGLE_DTLS_SCTP_1, Presence } from '../protocol/stanzas';
import { IQ, Jingle as JingleRequest, NS_JINGLE_DTLS_SCTP_1, Presence } from '../protocol';

let root: any;
try {

0 comments on commit c0c5d0c

Please sign in to comment.
You can’t perform that action at this time.