@forge/cache 0.1.0-next.1 → 0.2.0-next.0

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/README.md CHANGED
@@ -2,9 +2,7 @@ Library for Forge environment.
2
2
 
3
3
  Usage example:
4
4
  ```typescript
5
- import * as cache from "./rockmelon-api";
6
-
7
- const cacheClient = cache.connect();
5
+ import cache from "@forge/cache";
8
6
 
9
7
  const cacheClient = cache.connect();
10
8
 
@@ -2,6 +2,49 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const api_1 = require("@forge/api");
4
4
  const cache_1 = require("../cache");
5
+ describe('connect', () => {
6
+ let fetchMock;
7
+ describe('v1', () => {
8
+ beforeEach(() => {
9
+ fetchMock = jest.fn();
10
+ global['api'] = { requestRmsStore: fetchMock };
11
+ });
12
+ afterAll(() => {
13
+ delete global['api'];
14
+ });
15
+ it('chooses the v1 fetch based on runtime', () => {
16
+ const cacheClient = (0, cache_1.connect)();
17
+ expect(cacheClient['client']).toEqual(fetchMock);
18
+ });
19
+ });
20
+ describe('v2', () => {
21
+ let fetchMock;
22
+ let orgFetch;
23
+ beforeEach(() => {
24
+ fetchMock = jest.fn();
25
+ orgFetch = global['fetch'];
26
+ global['fetch'] = fetchMock;
27
+ global['__forge_runtime__'] = {
28
+ proxy: {
29
+ token: 'token',
30
+ url: 'https://proxy.atlassian.com'
31
+ },
32
+ rms: {
33
+ url: 'https://dev.services.atlassian.com',
34
+ host: 'rockmelon-storage.dev.atl-paas.net'
35
+ }
36
+ };
37
+ });
38
+ afterAll(() => {
39
+ global['fetch'] = orgFetch;
40
+ });
41
+ it('chooses the v2 fetch based on runtime', async () => {
42
+ const cacheClient = (0, cache_1.connect)();
43
+ await cacheClient['client']('asdf');
44
+ expect(fetchMock).toHaveBeenCalled();
45
+ });
46
+ });
47
+ });
5
48
  describe('createFetch', () => {
6
49
  let fetchMock;
7
50
  let orgFetch;
@@ -20,7 +63,7 @@ describe('createFetch', () => {
20
63
  url: 'https://proxy.atlassian.com'
21
64
  }
22
65
  };
23
- expect(cache_1.createFetch).toThrowError(new Error('RMS config not available.'));
66
+ expect(cache_1.createFetchRmsRuntimeV2).toThrowError(new Error('RMS config not available.'));
24
67
  });
25
68
  it('creates a fetch that adds the right headers and url', async () => {
26
69
  var _a;
@@ -34,7 +77,7 @@ describe('createFetch', () => {
34
77
  host: 'rockmelon-storage.dev.atl-paas.net'
35
78
  }
36
79
  };
37
- const fetch = (0, cache_1.createFetch)();
80
+ const fetch = (0, cache_1.createFetchRmsRuntimeV2)();
38
81
  await fetch('path');
39
82
  const [absoluteUrl, options] = (_a = fetchMock.mock.lastCall) !== null && _a !== void 0 ? _a : ['', {}];
40
83
  expect(absoluteUrl).toEqual('https://dev.services.atlassian.com/path');
@@ -48,14 +91,7 @@ describe('createFetch', () => {
48
91
  describe('Cache', () => {
49
92
  function buildCache() {
50
93
  const fetch = jest.fn();
51
- const cache = new cache_1.Cache({
52
- timer: () => ({
53
- measure: () => ({
54
- stop: () => {
55
- }
56
- })
57
- })
58
- }, fetch);
94
+ const cache = new cache_1.Cache(fetch);
59
95
  return { cache, fetch };
60
96
  }
61
97
  describe('set', () => {
package/out/cache.d.ts CHANGED
@@ -1,11 +1,9 @@
1
1
  import { RequestInit, Response as nodeFetchResponse } from 'node-fetch';
2
- import { Metrics } from '@forge/metrics';
3
2
  export declare type Response = Pick<nodeFetchResponse, 'text' | 'ok' | 'status'>;
4
3
  export declare function getResponseBody(response: Response): Promise<any>;
5
4
  export declare class Cache {
6
- private metrics;
7
5
  private client;
8
- constructor(metrics: Metrics, client: (path: string, options?: RequestInit) => Promise<Response>);
6
+ constructor(client: (path: string, options?: RequestInit) => Promise<Response>);
9
7
  private buildRequest;
10
8
  set(key: string, value: string, opt?: {
11
9
  ttlSeconds: number;
@@ -18,6 +16,7 @@ export declare class Cache {
18
16
  ttlSeconds: number;
19
17
  }): Promise<number>;
20
18
  }
21
- export declare function createFetch(): (path: string, options?: RequestInit) => Promise<Response>;
19
+ export declare function getFetchRmsRuntimeV1(): ((path: string, options?: RequestInit) => Promise<Response>) | undefined;
20
+ export declare function createFetchRmsRuntimeV2(): (path: string, options?: RequestInit) => Promise<Response>;
22
21
  export declare function connect(): Cache;
23
22
  //# sourceMappingURL=cache.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AACA,OAAkB,EAAE,WAAW,EAAE,QAAQ,IAAI,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAEnF,OAAO,EAAmB,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAG1D,oBAAY,QAAQ,GAAG,IAAI,CAAC,iBAAiB,EAAE,MAAM,GAAG,IAAI,GAAG,QAAQ,CAAC,CAAC;AAGzE,wBAAsB,eAAe,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,CAYtE;AAED,qBAAa,KAAK;IACJ,OAAO,CAAC,OAAO;IAAW,OAAO,CAAC,MAAM;gBAAhC,OAAO,EAAE,OAAO,EAAU,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC;IAEhH,OAAO,CAAC,YAAY;IAUP,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE;QAAE,UAAU,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAQ5E,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE;QAAE,UAAU,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC;IAS9F,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IASjC,eAAe,CAAC,GAAG,EAAE,MAAM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AO,CAAC,MAAM,CAAC;CAMzF;AAED,wBAAgB,oBAAoB,IAAI,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC,GAAG,SAAS,CAE/G;AAID,wBAAgB,uBAAuB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAwBpG;AAED,wBAAgB,OAAO,UAItB"}
package/out/cache.js CHANGED
@@ -1,9 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.connect = exports.createFetch = exports.Cache = exports.getResponseBody = void 0;
3
+ exports.connect = exports.createFetchRmsRuntimeV2 = exports.getFetchRmsRuntimeV1 = exports.Cache = exports.getResponseBody = void 0;
4
4
  const https_1 = require("https");
5
5
  const api_1 = require("@forge/api");
6
- const metrics_1 = require("@forge/metrics");
7
6
  async function getResponseBody(response) {
8
7
  const responseText = await response.text();
9
8
  if (!response.ok) {
@@ -18,8 +17,7 @@ async function getResponseBody(response) {
18
17
  }
19
18
  exports.getResponseBody = getResponseBody;
20
19
  class Cache {
21
- constructor(metrics, client) {
22
- this.metrics = metrics;
20
+ constructor(client) {
23
21
  this.client = client;
24
22
  }
25
23
  buildRequest(requestBody) {
@@ -32,38 +30,35 @@ class Cache {
32
30
  };
33
31
  }
34
32
  async set(key, value, opt) {
35
- const timer = this.metrics.timer('cache.set').measure();
36
- const response = await this.client('/rms/store/set', this.buildRequest(Object.assign({ key, value }, opt)));
37
- timer.stop();
33
+ const response = await this.client('rms/store/set', this.buildRequest(Object.assign({ key, value }, opt)));
38
34
  await getResponseBody(response);
39
35
  }
40
36
  async setIfNotExists(key, value, opt) {
41
- const timer = this.metrics.timer('cache.setIfNotExists').measure();
42
- const response = await this.client('/rms/store/setnx', this.buildRequest(Object.assign({ key, value }, opt)));
43
- timer.stop();
37
+ const response = await this.client('rms/store/setnx', this.buildRequest(Object.assign({ key, value }, opt)));
44
38
  const { response: result } = await getResponseBody(response);
45
39
  return result;
46
40
  }
47
41
  async get(key) {
48
- const timer = this.metrics.timer('cache.get').measure();
49
- const response = await this.client('/rms/store/get', this.buildRequest({ key }));
50
- timer.stop();
42
+ const response = await this.client('rms/store/get', this.buildRequest({ key }));
51
43
  const { value: result } = await getResponseBody(response);
52
44
  return result;
53
45
  }
54
46
  async incrementAndGet(key, opt) {
55
- const timer = this.metrics.timer('cache.incrementAndGet').measure();
56
- const response = await this.client('/rms/store/incr', this.buildRequest(Object.assign({ key }, opt)));
57
- timer.stop();
47
+ const response = await this.client('rms/store/incr', this.buildRequest(Object.assign({ key }, opt)));
58
48
  const { response: result } = await getResponseBody(response);
59
49
  return result;
60
50
  }
61
51
  }
62
52
  exports.Cache = Cache;
63
- function createFetch() {
53
+ function getFetchRmsRuntimeV1() {
54
+ var _a;
55
+ return (_a = global.api) === null || _a === void 0 ? void 0 : _a.requestRmsStore;
56
+ }
57
+ exports.getFetchRmsRuntimeV1 = getFetchRmsRuntimeV1;
58
+ function createFetchRmsRuntimeV2() {
64
59
  const { proxy, rms } = (0, api_1.getRuntime)();
65
60
  const agent = new https_1.Agent({ keepAlive: true });
66
- if (!rms) {
61
+ if (!(rms === null || rms === void 0 ? void 0 : rms.url) || !(rms === null || rms === void 0 ? void 0 : rms.host)) {
67
62
  throw new Error('RMS config not available.');
68
63
  }
69
64
  return async function (path, options) {
@@ -71,8 +66,10 @@ function createFetch() {
71
66
  return await global['fetch'](rmsFullUrl, Object.assign(Object.assign({}, options), { agent, headers: Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.headers), { Authorization: `Bearer ${proxy.token}`, Host: rms.host }) }));
72
67
  };
73
68
  }
74
- exports.createFetch = createFetch;
69
+ exports.createFetchRmsRuntimeV2 = createFetchRmsRuntimeV2;
75
70
  function connect() {
76
- return new Cache(metrics_1.internalMetrics, createFetch());
71
+ var _a;
72
+ const fetch = (_a = getFetchRmsRuntimeV1()) !== null && _a !== void 0 ? _a : createFetchRmsRuntimeV2();
73
+ return new Cache(fetch);
77
74
  }
78
75
  exports.connect = connect;
package/out/index.d.ts CHANGED
@@ -1,2 +1,6 @@
1
- export { connect } from './cache';
1
+ import { connect } from './cache';
2
+ declare const _default: {
3
+ connect: typeof connect;
4
+ };
5
+ export default _default;
2
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;;;;AAClC,wBAA2B"}
package/out/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.connect = void 0;
4
- var cache_1 = require("./cache");
5
- Object.defineProperty(exports, "connect", { enumerable: true, get: function () { return cache_1.connect; } });
3
+ const cache_1 = require("./cache");
4
+ exports.default = { connect: cache_1.connect };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forge/cache",
3
- "version": "0.1.0-next.1",
3
+ "version": "0.2.0-next.0",
4
4
  "description": "Forge Cache methods",
5
5
  "author": "Atlassian",
6
6
  "license": "UNLICENSED",
@@ -20,7 +20,6 @@
20
20
  "node-fetch": "2.6.7"
21
21
  },
22
22
  "dependencies": {
23
- "@forge/api": "^2.16.0-next.2",
24
- "@forge/metrics": "^0.1.9-next.3"
23
+ "@forge/api": "^2.16.0"
25
24
  }
26
25
  }