@sockethub/activity-streams 4.4.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +210 -0
- package/dist/activity-streams.js +462 -0
- package/dist/activity-streams.min.js +19 -0
- package/package.json +58 -0
- package/src/activity-streams.test.ts +302 -0
- package/src/activity-streams.ts +344 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014 Nick Jennings
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# @sockethub/activity-streams
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.org/package/@sockethub/activity-streams)
|
|
4
|
+
[](https://npmjs.org/package/@sockethub/activity-streams)
|
|
5
|
+
|
|
6
|
+
A simple tool to facilitate handling and referencing activity streams and its objects, cutting
|
|
7
|
+
down on verbosity.
|
|
8
|
+
|
|
9
|
+
Designed to run in both `node.js`, `bun` and the `browser`.
|
|
10
|
+
|
|
11
|
+
This is a WIP and not fully JSON-LD or ActivityStreams2 compliant, suggestions for improvement
|
|
12
|
+
are very welcome.
|
|
13
|
+
|
|
14
|
+
## What it does
|
|
15
|
+
|
|
16
|
+
This library helps you:
|
|
17
|
+
|
|
18
|
+
- **Create and store** ActivityStream objects with validation
|
|
19
|
+
- **Auto-expand** string references into full objects (e.g., `"user@example.com"` becomes `{id: "user@example.com"}`)
|
|
20
|
+
- **Build Activity Streams** that automatically link to stored objects
|
|
21
|
+
- **Listen for events** when objects are created or deleted
|
|
22
|
+
|
|
23
|
+
## Install
|
|
24
|
+
|
|
25
|
+
### Node.js
|
|
26
|
+
|
|
27
|
+
`$ npm install @sockethub/activity-streams`
|
|
28
|
+
|
|
29
|
+
### Bun
|
|
30
|
+
|
|
31
|
+
`$ bun install @sockethub/activity-streams`
|
|
32
|
+
|
|
33
|
+
#### CommonJS
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
const ASFactory = require('@sockethub/activity-streams');
|
|
37
|
+
const ActivityStreams = ASFactory({
|
|
38
|
+
failOnUnknownObjectProperties: false // default
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
#### ESM
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
import { ASFactory } from '@sockethub/activity-streams';
|
|
46
|
+
const ActivityStreams = ASFactory({
|
|
47
|
+
failOnUnknownObjectProperties: false // default
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Browser
|
|
52
|
+
|
|
53
|
+
The browser bundle is available in the dist folder:
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
import '@sockethub/activity-streams/dist/activity-streams.js';
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
You can place it somewhere accessible from the web and include it via a `script` tag.
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
<script src="http://example.com/activity-streams.js" type="module"></script>
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Once included in a web-page, the `ActivityStreams` base object should be on the global scope, with
|
|
66
|
+
the sub-properties `ActivityStreams.Object` and `ActivityStreams.Stream`.
|
|
67
|
+
|
|
68
|
+
## Quick Start
|
|
69
|
+
|
|
70
|
+
### Standalone Usage
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
import { ASFactory } from '@sockethub/activity-streams';
|
|
74
|
+
const ActivityStreams = ASFactory();
|
|
75
|
+
|
|
76
|
+
// Create an object
|
|
77
|
+
const user = ActivityStreams.Object.create({
|
|
78
|
+
id: "user@example.com",
|
|
79
|
+
type: "person",
|
|
80
|
+
name: "John Doe"
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// Create an activity that references it
|
|
84
|
+
const activity = ActivityStreams.Stream({
|
|
85
|
+
type: "send",
|
|
86
|
+
context: "irc",
|
|
87
|
+
actor: "user@example.com", // automatically expands to full object
|
|
88
|
+
object: { type: "message", content: "Hello!" }
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### With Sockethub Client
|
|
93
|
+
|
|
94
|
+
If you're using the Sockethub client, this library is already bundled and available:
|
|
95
|
+
|
|
96
|
+
```javascript
|
|
97
|
+
import SockethubClient from '@sockethub/client';
|
|
98
|
+
|
|
99
|
+
const socket = io('http://localhost:10550');
|
|
100
|
+
const sockethubClient = new SockethubClient(socket);
|
|
101
|
+
|
|
102
|
+
// Access ActivityStreams through the client
|
|
103
|
+
const user = sockethubClient.ActivityStreams.Object.create({
|
|
104
|
+
id: "user@example.com",
|
|
105
|
+
type: "person",
|
|
106
|
+
name: "John Doe"
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
const activity = sockethubClient.ActivityStreams.Stream({
|
|
110
|
+
type: "send",
|
|
111
|
+
context: "irc",
|
|
112
|
+
actor: "user@example.com",
|
|
113
|
+
object: { type: "message", content: "Hello!" }
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Example
|
|
118
|
+
|
|
119
|
+
```javascript
|
|
120
|
+
|
|
121
|
+
const ActivityStreams = ASFactory();
|
|
122
|
+
|
|
123
|
+
ActivityStreams.Object.create({
|
|
124
|
+
id: 'irc://exampleUser@irc.freenode.net',
|
|
125
|
+
type: "person",
|
|
126
|
+
name: 'Example User',
|
|
127
|
+
url: "http://activitystrea.ms",
|
|
128
|
+
image: {
|
|
129
|
+
url: "http://activitystrea.ms/avatar.jpg",
|
|
130
|
+
mediaType: "image/jpeg",
|
|
131
|
+
width: 250,
|
|
132
|
+
height: 250
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
ActivityStreams.on('activity-object-create', function (obj) {
|
|
137
|
+
console.log('this object was just created: ', obj);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
ActivityStreams.Object.create({
|
|
141
|
+
id: 'irc://irc.freenode.net/activitystreams',
|
|
142
|
+
type: "chatroom",
|
|
143
|
+
name: '#activitystreams'
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const exampleUser = ActivityStreams.Object.get('irc://exampleUser@irc.freenode.net');
|
|
147
|
+
// ... returns:
|
|
148
|
+
// {
|
|
149
|
+
// id: 'irc://exampleUser@irc.freenode.net',
|
|
150
|
+
// type: "person",
|
|
151
|
+
// name: 'Example User',
|
|
152
|
+
// url: "http://activitystrea.ms",
|
|
153
|
+
// image: {
|
|
154
|
+
// url: "http://activitystrea.ms/avatar.jpg",
|
|
155
|
+
// mediaType: "image/jpeg",
|
|
156
|
+
// width: 250,
|
|
157
|
+
// height: 250
|
|
158
|
+
// }
|
|
159
|
+
// }
|
|
160
|
+
|
|
161
|
+
ActivityStreams.Stream({
|
|
162
|
+
type: 'send',
|
|
163
|
+
actor: 'irc://exampleUser@irc.freenode.net',
|
|
164
|
+
object: {
|
|
165
|
+
type: "message",
|
|
166
|
+
content: "hello world!"
|
|
167
|
+
},
|
|
168
|
+
target: 'irc://irc.freenode.net/activitystreams'
|
|
169
|
+
});
|
|
170
|
+
// ... returns:
|
|
171
|
+
// {
|
|
172
|
+
// type: 'send',
|
|
173
|
+
// actor: {
|
|
174
|
+
// id: 'irc://exampleUser@irc.freenode.net',
|
|
175
|
+
// type: "person",
|
|
176
|
+
// name: 'Example User',
|
|
177
|
+
// url: "http://activitystrea.ms",
|
|
178
|
+
// image: {
|
|
179
|
+
// url: "http://activitystrea.ms/avatar.jpg",
|
|
180
|
+
// mediaType: "image/jpeg",
|
|
181
|
+
// width: 250,
|
|
182
|
+
// height: 250
|
|
183
|
+
// }
|
|
184
|
+
// },
|
|
185
|
+
// object: {
|
|
186
|
+
// type: "message",
|
|
187
|
+
// content: "hello world!"
|
|
188
|
+
// },
|
|
189
|
+
// target: {
|
|
190
|
+
// id: 'irc://irc.freenode.net/activitystreams',
|
|
191
|
+
// type: "chatroom",
|
|
192
|
+
// name: '#activitystreams'
|
|
193
|
+
// }
|
|
194
|
+
// }
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Error Handling
|
|
198
|
+
|
|
199
|
+
The library provides clear error messages when validation fails:
|
|
200
|
+
|
|
201
|
+
```javascript
|
|
202
|
+
// This will throw: "ActivityStreams validation failed: the 'object' property is null"
|
|
203
|
+
ActivityStreams.Object.create(null);
|
|
204
|
+
|
|
205
|
+
// This will throw: "ActivityStreams validation failed: the 'object' property received string..."
|
|
206
|
+
ActivityStreams.Object.create("just-a-string");
|
|
207
|
+
|
|
208
|
+
// This will throw: "ActivityStreams validation failed: property 'foo' with value 'bar' is not allowed..."
|
|
209
|
+
ActivityStreams.Object.create({ id: "test", foo: "bar" });
|
|
210
|
+
```
|