@miso.ai/server-wordpress 0.6.3-beta.1 → 0.6.3-beta.3

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/cli/entities.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Transform } from 'stream';
2
+ import split2 from 'split2';
2
3
  import { stream, parseDuration } from '@miso.ai/server-commons';
3
4
  import { WordPressClient } from '../src/index.js';
4
5
  import { normalizeOptions, normalizeTransform, parseDate } from './utils.js';
@@ -76,6 +77,12 @@ async function run({ subcmd, count, terms, update, name, ...options }) {
76
77
  case 'count':
77
78
  await runCount(client, name, options);
78
79
  return;
80
+ case 'absence':
81
+ await runPresence(client, name, { present: false });
82
+ return;
83
+ case 'presence':
84
+ await runPresence(client, name, { present: true });
85
+ return;
79
86
  }
80
87
  if (count) {
81
88
  await runCount(client, name, options);
@@ -136,6 +143,17 @@ export async function runUpdate(client, name, update, options) {
136
143
  );
137
144
  }
138
145
 
146
+ export async function runPresence(client, name, options) {
147
+ await stream.pipeline(
148
+ process.stdin,
149
+ split2(),
150
+ client.entities(name).presence(options),
151
+ new stream.OutputStream({
152
+ objectMode: false,
153
+ }),
154
+ );
155
+ }
156
+
139
157
  async function buildUpdateStream(client, name, update, {
140
158
  date, after, before, orderBy, order, // strip off date filters and order criteria
141
159
  transform,
package/cli/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { yargs } from '@miso.ai/server-commons';
3
3
  import version from '../src/version.js';
4
- import profile from './profile.js';
4
+ import { profile, init } from './profile.js';
5
5
  import taxonomies from './taxonomies.js';
6
6
  import entities from './entities.js';
7
7
 
@@ -21,6 +21,7 @@ yargs.build(yargs => {
21
21
  default: false,
22
22
  })
23
23
  .hide('debug')
24
+ .command(init)
24
25
  .command(profile)
25
26
  .command(taxonomies)
26
27
  .command(entities)
package/cli/profile.js CHANGED
@@ -11,7 +11,7 @@ function build(yargs) {
11
11
  });
12
12
  }
13
13
 
14
- async function run({ generate, ...options }) {
14
+ async function runProfile({ generate, ...options }) {
15
15
  if (generate) {
16
16
  await runGenerate(options);
17
17
  } else {
@@ -38,9 +38,19 @@ async function runView(options) {
38
38
  console.log(JSON.stringify(client.profile, undefined, 2));
39
39
  }
40
40
 
41
- export default {
41
+ async function runInit(options) {
42
+ await runGenerate(options);
43
+ }
44
+
45
+ export const profile = {
42
46
  command: 'profile',
43
47
  desc: 'WordPress site profile management',
44
48
  builder: build,
45
- handler: run,
49
+ handler: runProfile,
50
+ };
51
+
52
+ export const init = {
53
+ command: 'init <site>',
54
+ desc: 'Initialize WordPress site profile',
55
+ handler: runInit,
46
56
  };
package/package.json CHANGED
@@ -17,9 +17,9 @@
17
17
  "simonpai <simon.pai@askmiso.com>"
18
18
  ],
19
19
  "dependencies": {
20
- "@miso.ai/server-commons": "0.6.3-beta.1",
20
+ "@miso.ai/server-commons": "0.6.3-beta.3",
21
21
  "axios": "^0.27.2",
22
22
  "axios-retry": "^3.3.1"
23
23
  },
24
- "version": "0.6.3-beta.1"
24
+ "version": "0.6.3-beta.3"
25
25
  }
package/src/axios.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import axios from 'axios';
2
2
  import axiosRetry from 'axios-retry';
3
3
 
4
+ // TODO: create an instance
5
+
4
6
  axiosRetry(axios, { retries: 5, retryDelay: count => count * 300 });
5
7
 
6
8
  export default axios;
@@ -2,7 +2,7 @@ import { asArray, Resolution } from '@miso.ai/server-commons';
2
2
 
3
3
  export default class EntityIndex {
4
4
 
5
- constructor(entities, { process, value } = {}) {
5
+ constructor(entities, { process, value, fields } = {}) {
6
6
  this._entities = entities;
7
7
  if (process) {
8
8
  this._process = process;
@@ -10,6 +10,7 @@ export default class EntityIndex {
10
10
  if (value) {
11
11
  this._value = (en => en && value(en)); // null-safe
12
12
  }
13
+ this._fields = fields;
13
14
  this.name = entities.name;
14
15
  this._index = new Map();
15
16
  this._notFound = new Set();
@@ -66,7 +67,7 @@ export default class EntityIndex {
66
67
  if (idsToFetch.length > 0) {
67
68
  (async () => {
68
69
  const idsFetchSet = new Set(idsToFetch);
69
- const stream = await this._entities.stream({ ids: idsToFetch });
70
+ const stream = await this._entities.stream({ ids: idsToFetch, fields: this._fields });
70
71
  for await (const entity of stream) {
71
72
  const { id } = entity;
72
73
  this._index.set(id, this._process(entity));
@@ -2,6 +2,7 @@ import { Transform } from 'stream';
2
2
  import { asArray, stream } from '@miso.ai/server-commons';
3
3
  import EntityIndex from './entity-index.js';
4
4
  import EntityTransformStream from './transform.js';
5
+ import EntityPresenceStream from './presence.js';
5
6
  import defaultTransform from './transform-default.js';
6
7
  import legacyTransform from './transform-legacy.js';
7
8
 
@@ -82,6 +83,10 @@ export default class Entities {
82
83
  return this._client._helpers.terms(this.name, options);
83
84
  }
84
85
 
86
+ presence(options) {
87
+ return new EntityPresenceStream(this._client, this.name, options);
88
+ }
89
+
85
90
  get index() {
86
91
  return this._index;
87
92
  }
@@ -0,0 +1,106 @@
1
+ import { Transform } from 'stream';
2
+ import axios from '../axios.js';
3
+
4
+ export default class EntityPresenceStream extends Transform {
5
+
6
+ constructor(client, name, {
7
+ present = true,
8
+ fetchSize = 20,
9
+ preserveOrder = true,
10
+ } = {}) {
11
+ super();
12
+ this._client = client;
13
+ this._name = name;
14
+ this._options = {
15
+ present,
16
+ fetchSize,
17
+ preserveOrder,
18
+ }
19
+ this._inputs = [];
20
+ this._pendingSet = new Set();
21
+ this._requests = [];
22
+ this._map = new Map();
23
+ this._done = false;
24
+ }
25
+
26
+ async _transform(id, _, next) {
27
+ id = `${id}`; // buffer -> string
28
+ if (id) {
29
+ this._inputs.push(id);
30
+ this._outputAll();
31
+ this._requestAll();
32
+ }
33
+ next();
34
+ }
35
+
36
+ _flush(done) {
37
+ this._done = done;
38
+ this._outputAll();
39
+ if (this._inputs.length > 0) {
40
+ this._requestAll(true);
41
+ }
42
+ }
43
+
44
+ _outputAll() {
45
+ // TODO: implement when preserveOrder = false
46
+ let i = 0;
47
+ for (const len = this._inputs.length; i < len; i++) {
48
+ const id = this._inputs[i];
49
+ const entry = this._map.get(id);
50
+ if (!entry || entry.value === undefined) {
51
+ break;
52
+ }
53
+ if (this._options.present === entry.value) {
54
+ this.push(id);
55
+ }
56
+ }
57
+ if (i > 0) {
58
+ this._inputs = this._inputs.slice(i);
59
+ }
60
+ if (this._done && this._inputs.length === 0) {
61
+ this._done();
62
+ }
63
+ }
64
+
65
+ _requestAll(flush = false) {
66
+ for (const id of this._inputs) {
67
+ this._fetchAll();
68
+ if (!this._map.has(id)) {
69
+ this._map.set(id, { status: 'pending' });
70
+ this._pendingSet.add(id);
71
+ }
72
+ }
73
+ this._fetchAll(flush);
74
+ }
75
+
76
+ async _fetchAll(flush = false) {
77
+ if (!flush && this._pendingSet.size < this._options.fetchSize) {
78
+ return;
79
+ }
80
+ const ids = Array.from(this._pendingSet);
81
+ for (const id of ids) {
82
+ this._map.get(id).status = 'fetching';
83
+ }
84
+ this._pendingSet = new Set();
85
+
86
+ const presences = await this._fetch(ids);
87
+
88
+ for (const id of ids) {
89
+ const entry = this._map.get(id);
90
+ entry.status = 'ready';
91
+ entry.value = presences.has(id);
92
+ }
93
+ this._outputAll();
94
+ }
95
+
96
+ async _fetch(ids) {
97
+ const url = await this._client._helpers.url.build(this._name, { include: ids, fields: ['id'] });
98
+ const { data } = await axios.get(url);
99
+ const presences = new Set();
100
+ for (const { id } of data) {
101
+ presences.add(`${id}`);
102
+ }
103
+ return presences;
104
+ }
105
+
106
+ }
package/src/version.js CHANGED
@@ -1 +1 @@
1
- export default '0.6.3-beta.1';
1
+ export default '0.6.3-beta.3';