All files / app/mods/sync origin.ts

68.42% Statements 13/19
37.03% Branches 10/27
50% Functions 1/2
80% Lines 12/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335118x       118x   118x                                                                                                   118x                                                                                                                                                                                                                                                                                     118x                                                                                                                                         118x                                                                                                 8x 8x 8x 8x 2x                 118x                
import { DateTime } from 'luxon';
import { Plugin } from '../../model/plugin';
import { Ref } from '../../model/ref';
import { Mod } from '../../model/tag';
import { hasTag } from '../../util/tag';
 
export const originPlugin: Plugin = {
  tag: '+plugin/origin',
  name: $localize`🏛️ Remote Origin`,
  config: {
    mod: $localize`🏛️ Origin`,
    version: 1,
    default: true,
    generated: $localize`Generated by jasper-ui ${DateTime.now().toISO()}`,
    submit: $localize`🏛️ origin`,
    settings: $localize`origin`,
    icons: [{ label: $localize`🏛️`, order: 3 }],
    description: $localize`Replicate a remote Jasper instance. The remote
     origin will be scraped on an interval you specify.
     If the remote is also set up to replicate from this instance, you may
     communicate with remote users.
     You may configure if metadata is generated or plugins are validated. `,
    sorts: [
      { sort: 'origin', label: $localize`🏛️ origin` },
      { sort: 'origin:len', label: $localize`🪆 nesting` },
    ],
    form: [{
      key: 'local',
      type: 'origin',
      props: {
        label: $localize`Local:`
      }
    }, {
      key: 'remote',
      type: 'origin',
      props: {
        label: $localize`Remote:`
      }
    }],
    advancedForm: [{
      key: 'proxy',
      type: 'url',
      props: {
        label: $localize`Proxy:`
      }
    }],
  },
  schema: {
    optionalProperties: {
      local: { type: 'string' },
      remote: { type: 'string' },
      proxy: { type: 'string' },
    },
  },
};
 
export const originPullPlugin: Plugin = {
  tag: '+plugin/origin/pull',
  name: $localize`🏛️📥️ Remote Origin Pull`,
  config: {
    mod: $localize`🏛️ Origin`,
    version: 1,
    default: true,
    generated: $localize`Generated by jasper-ui ${DateTime.now().toISO()}`,
    submitChild: $localize`📥️ pull`,
    description: $localize`Replicate a remote Jasper instance. The remote
      origin will be scraped on an interval you specify.
      If the remote is also set up to replicate from this instance, you may
      communicate with remote users.
      You may configure if metadata is generated or plugins are validated. `,
    icons: [
      { label: $localize`📥️` },
      { tag: '-+plugin/cron', label: $localize`🚫️`, title: $localize`Pulling disabled`, order: -1 },
      { tag: '+plugin/cron', condition: 'websocket', label: $localize`📶️`, title: $localize`Streaming updates`, order: -1 },
    ],
    actions: [
      { tag: '+plugin/cron', labelOn: $localize`disable`, labelOff: $localize`enable` },
      { response: '+plugin/user/run', labelOn: $localize`cancel`, title: $localize`Cancel pulling.` },
    ],
    advancedActions: [
      { response: '+plugin/user/run', labelOff: $localize`pull`, title: $localize`Pull a batch of updates from the remote.`, confirm: $localize`Are you sure you want to pull?` },
    ],
    // language=Handlebars
    infoUi: `
      {{#if (interestingTags addTags)}} tagging refs {{/if}}
      {{#each (interestingTags addTags)}}
        #{{.}}
      {{/each}}`,
    form: [{
      key: 'query',
      type: 'query'
    }, {
      key: 'addTags',
      type: 'tags',
      props: {
        label: $localize`Add Tags:`,
      },
    }, {
      key: 'websocket',
      type: 'boolean',
      defaultValue: true,
      props: {
        label: $localize`Stream Updates:`,
      },
    }, {
      key: 'cacheProxy',
      type: 'boolean',
      props: {
        label: $localize`Proxy:`,
      },
    }, {
      key: 'cacheProxyPrefetch',
      type: 'boolean',
      props: {
        label: $localize`Prefetch Proxy:`,
      },
      expressions: {
        hide: '!model.cacheProxy',
      },
    }],
    advancedForm: [{
      key: 'cachePrefetch',
      type: 'boolean',
      props: {
        label: $localize`Prefetch Files:`,
      },
    }, {
      key: 'removeTags',
      type: 'tags',
      props: {
        label: $localize`Remove Tags:`,
        addText: $localize`+ Remove another tag`,
      },
    }, {
      key: 'batchSize',
      type: 'integer',
      defaultValue: 250,
      props: {
        label: $localize`Batch Size:`,
      },
    }, {
      key: 'validatePlugins',
      type: 'boolean',
      props: {
        label: $localize`Validate Plugins:`,
      },
    }, {
      key: 'stripInvalidPlugins',
      type: 'boolean',
      props: {
        label: $localize`Strip Invalid Plugins:`,
      },
      expressions: {
        hide: '!model.validatePlugins',
      },
    }, {
      key: 'validateTemplates',
      type: 'boolean',
      props: {
        label: $localize`Validate Templates:`,
      },
    }, {
      key: 'stripInvalidTemplates',
      type: 'boolean',
      props: {
        label: $localize`Strip Invalid Templates:`,
      },
      expressions: {
        hide: '!model.validateTemplates',
      },
    }],
  },
  defaults: {
    cachePrefetch: true,
    websocket: true,
  },
  schema: {
    optionalProperties: {
      cachePrefetch: { type: 'boolean' },
      cacheProxy: { type: 'boolean' },
      cacheProxyPrefetch: { type: 'boolean' },
      websocket: { type: 'boolean' },
      query: { type: 'string' },
      batchSize: { type: 'int32' },
      validatePlugins: { type: 'boolean' },
      stripInvalidPlugins: { type: 'boolean' },
      validateTemplates: { type: 'boolean' },
      stripInvalidTemplates: { type: 'boolean' },
      originFromTag: { type: 'string' },
      addTags: { elements: { type: 'string' } },
      removeTags: { elements: { type: 'string' } },
    },
  },
};
 
export const originPushPlugin: Plugin = {
  tag: '+plugin/origin/push',
  name: $localize`🏛️📤️ Remote Origin Push`,
  config: {
    mod: $localize`🏛️ Origin`,
    version: 1,
    default: true,
    generated: $localize`Generated by jasper-ui ${DateTime.now().toISO()}`,
    submitChild: $localize`📤️ push`,
    description: $localize`Push modifications to a remote origin.
      On the scheduled interval set, the server will check if the remote cursor is
      behind the local cursor. If "Push on change" is set changes are pushed immediately.`,
    icons: [
      { label: $localize`📤️` },
      { tag: '-+plugin/cron', label: $localize`🚫️`, title: $localize`Pushing disabled`, order: -1 },
      { tag: '+plugin/cron', condition: 'pushOnChange', label: $localize`📶️`, title: $localize`Pushing on change`, order: -1 },
    ],
    actions: [
      { tag: '+plugin/cron', labelOn: $localize`disable`, labelOff: $localize`enable` },
      { response: '+plugin/user/run', labelOn: $localize`cancel`, title: $localize`Cancel pushing` },
    ],
    advancedActions: [
      { response: '+plugin/user/run', labelOff: $localize`push`, title: $localize`Push a batch of updates to the remote.`, confirm: $localize`Are you sure you want to push?` },
    ],
    // language=Handlebars
    infoUi: `
      {{#if (and (hasTag '+plugin/origin/pull' ref) (hasTag '+plugin/origin/push' ref))}}
        <span title="Pushing and Pulling is not recommended">⛔️</span>
      {{/if}}`,
    form: [{
      key: 'pushOnChange',
      type: 'boolean',
      defaultValue: true,
      props: {
        label: $localize`Push on change`,
      }
    }, {
      key: 'cache',
      type: 'boolean',
      props: {
        label: $localize`Push cache`,
      }
    }],
    advancedForm: [{
      key: 'query',
      type: 'query'
    }, {
      key: 'batchSize',
      type: 'integer',
      defaultValue: 250,
      props: {
        label: $localize`Batch Size:`,
      }
    }],
  },
  defaults: {
    pushOnChange: true,
    cache: true,
  },
  schema: {
    optionalProperties: {
      pushOnChange: { type: 'boolean' },
      cache: { type: 'boolean' },
      query: { type: 'string' },
      batchSize: { type: 'int32' },
    },
  },
};
 
export const originTunnelPlugin: Plugin = {
  tag: '+plugin/origin/tunnel',
  name: $localize`🏛️🕳️️️ Origin Tunnel`,
  config: {
    mod: $localize`🏛️ Origin`,
    version: 1,
    default: true,
    generated: $localize`Generated by jasper-ui ${DateTime.now().toISO()}`,
    submitChild: $localize`🕳️️️ tunnel`,
    icons: [{ label: $localize`🕳️️️` }],
    description: $localize`Create an SSH tunnel`,
    advancedForm: [{
      key: 'remoteUser',
      type: 'quser',
      props: {
        label: $localize`Remote User:`,
      },
    }, {
      key: 'sshHost',
      type: 'input',
      props: {
        label: $localize`SSH Host:`,
      },
    }, {
      key: 'sshPort',
      type: 'number',
      props: {
        label: $localize`SSH Port:`,
        min: 22,
      },
    }, {
      key: 'hostFingerprint',
      type: 'string',
      props: {
        label: $localize`Host Fingerprint:`
      }
    }],
  },
  schema: {
    optionalProperties: {
      hostFingerprint: { type: 'string' },
      remoteUser: { type: 'string' },
      sshHost: { type: 'string' },
      sshPort: { type: 'uint32' },
    },
  },
};
 
export function isReplicating(local: string, remote: Ref, apis: Map<string, string>) {
  Iif (!hasTag('+plugin/origin/pull', remote)) return false;
  const plugin = remote.plugins?.['+plugin/origin'];
  Iif (plugin?.proxy && apis.has(plugin.proxy)) return apis.get(plugin.proxy) === (plugin.remote || '');
  if (apis.has(remote.url)) return apis.get(remote.url) === (plugin?.remote || '');
  return false;
}
 
export function isPushing(remote: Ref, subOrigin = '') {
  if (!hasTag('+plugin/origin/push', remote)) return false;
  const plugin = remote.plugins?.['+plugin/origin'];
  return (plugin?.local || '') === subOrigin;
}
 
export const remoteOriginMod: Mod = {
  plugin: [
    originPlugin,
    originPushPlugin,
    originPullPlugin,
    originTunnelPlugin,
  ],
};