@forge/cache 0.7.0-next.0 → 0.7.0-next.2

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
@@ -21,4 +21,10 @@ const result = await cacheClient.decrementAndGet("hello");
21
21
  const result = await cacheClient.delete("hello");
22
22
 
23
23
  const result = await cacheClient.scan("hello*", { cursor: "10", count: 10 });
24
+
25
+ const result = await cacheClient.leftPush("list", "3");
26
+
27
+ const result = await cacheClient.rightPop("list");
28
+
29
+ const result = await cacheClient.listLength("list");
24
30
  ```
@@ -1,16 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
5
3
  const api_1 = require("@forge/api");
6
4
  const cache_1 = require("../cache");
7
- jest.mock('node-fetch');
8
- const fetchMock = node_fetch_1.default;
9
5
  describe('connect', () => {
6
+ let fetchMock;
10
7
  describe('v1', () => {
11
8
  beforeEach(() => {
12
- jest.resetAllMocks();
13
- jest.clearAllMocks();
9
+ fetchMock = jest.fn();
14
10
  global['api'] = { requestRmsStore: fetchMock };
15
11
  });
16
12
  afterAll(() => {
@@ -22,7 +18,12 @@ describe('connect', () => {
22
18
  });
23
19
  });
24
20
  describe('v2', () => {
21
+ let fetchMock;
22
+ let orgFetch;
25
23
  beforeEach(() => {
24
+ fetchMock = jest.fn();
25
+ orgFetch = global['fetch'];
26
+ global['fetch'] = fetchMock;
26
27
  global['__forge_runtime__'] = {
27
28
  proxy: {
28
29
  token: 'token',
@@ -38,6 +39,9 @@ describe('connect', () => {
38
39
  }
39
40
  };
40
41
  });
42
+ afterAll(() => {
43
+ global['fetch'] = orgFetch;
44
+ });
41
45
  it('chooses the v2 fetch based on runtime', async () => {
42
46
  const cacheClient = (0, cache_1.connect)();
43
47
  await cacheClient['client']('asdf');
@@ -46,6 +50,16 @@ describe('connect', () => {
46
50
  });
47
51
  });
48
52
  describe('createFetch', () => {
53
+ let fetchMock;
54
+ let orgFetch;
55
+ beforeEach(() => {
56
+ fetchMock = jest.fn();
57
+ orgFetch = global['fetch'];
58
+ global['fetch'] = fetchMock;
59
+ });
60
+ afterAll(() => {
61
+ global['fetch'] = orgFetch;
62
+ });
49
63
  it('fetch fails when rms config is not available', async () => {
50
64
  global['__forge_runtime__'] = {
51
65
  proxy: {
@@ -75,7 +89,7 @@ describe('createFetch', () => {
75
89
  await fetch('path');
76
90
  const [absoluteUrl, options] = (_a = fetchMock.mock.lastCall) !== null && _a !== void 0 ? _a : ['', {}];
77
91
  expect(absoluteUrl).toEqual('https://dev.services.atlassian.com/path');
78
- expect((options === null || options === void 0 ? void 0 : options.agent)['passthrough']['keepAlive']).toBeTruthy();
92
+ expect((options === null || options === void 0 ? void 0 : options.agent)['keepAlive']).toBeTruthy();
79
93
  expect(options === null || options === void 0 ? void 0 : options.headers).toMatchObject({
80
94
  Authorization: 'Bearer token',
81
95
  Host: 'rockmelon-storage.dev.atl-paas.net',
@@ -291,4 +305,63 @@ describe('Cache', () => {
291
305
  expect(fetch.mock.lastCall).toMatchSnapshot();
292
306
  });
293
307
  });
308
+ describe('leftPush', () => {
309
+ it('handles success', async () => {
310
+ const { cache, fetch } = buildCache();
311
+ fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 1 }), status: 200 });
312
+ const result = await cache.leftPush('list', 'value');
313
+ expect(result).toEqual(1);
314
+ expect(fetch.mock.lastCall).toMatchSnapshot();
315
+ });
316
+ it('handles failure', async () => {
317
+ const { cache, fetch } = buildCache();
318
+ fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
319
+ await expect(cache.leftPush('list', 'value')).rejects.toMatchError(new api_1.HttpError('403: Not allowed'));
320
+ expect(fetch.mock.lastCall).toMatchSnapshot();
321
+ });
322
+ });
323
+ describe('rightPop', () => {
324
+ it('handles success', async () => {
325
+ const { cache, fetch } = buildCache();
326
+ fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 'value' }), status: 200 });
327
+ const result = await cache.rightPop('list');
328
+ expect(result).toEqual('value');
329
+ expect(fetch.mock.lastCall).toMatchSnapshot();
330
+ });
331
+ it('handles success when key does not exist', async () => {
332
+ const { cache, fetch } = buildCache();
333
+ fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: null }), status: 200 });
334
+ const result = await cache.rightPop('list');
335
+ expect(result).toBeNull();
336
+ expect(fetch.mock.lastCall).toMatchSnapshot();
337
+ });
338
+ it('handles failure', async () => {
339
+ const { cache, fetch } = buildCache();
340
+ fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
341
+ await expect(cache.rightPop('list')).rejects.toMatchError(new api_1.HttpError('403: Not allowed'));
342
+ expect(fetch.mock.lastCall).toMatchSnapshot();
343
+ });
344
+ });
345
+ describe('listLength', () => {
346
+ it('handles success', async () => {
347
+ const { cache, fetch } = buildCache();
348
+ fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 2 }), status: 200 });
349
+ const result = await cache.listLength('list');
350
+ expect(result).toBe(2);
351
+ expect(fetch.mock.lastCall).toMatchSnapshot();
352
+ });
353
+ it('handles success when key does not exist', async () => {
354
+ const { cache, fetch } = buildCache();
355
+ fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 0 }), status: 200 });
356
+ const result = await cache.listLength('list');
357
+ expect(result).toBe(0);
358
+ expect(fetch.mock.lastCall).toMatchSnapshot();
359
+ });
360
+ it('handles failure', async () => {
361
+ const { cache, fetch } = buildCache();
362
+ fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
363
+ await expect(cache.listLength('list')).rejects.toMatchError(new api_1.HttpError('403: Not allowed'));
364
+ expect(fetch.mock.lastCall).toMatchSnapshot();
365
+ });
366
+ });
294
367
  });
package/out/cache.d.ts CHANGED
@@ -26,6 +26,9 @@ export declare class Cache {
26
26
  cursor?: string;
27
27
  count?: number;
28
28
  }): Promise<ScanResult>;
29
+ leftPush(key: string, value: string): Promise<number>;
30
+ rightPop(key: string): Promise<string | null>;
31
+ listLength(key: string): Promise<number>;
29
32
  }
30
33
  export declare function getFetchRmsRuntimeV1(): ((path: string, options?: RequestInit) => Promise<Response>) | undefined;
31
34
  export declare function createFetchRmsRuntimeV2(): (path: string, options?: RequestInit) => Promise<Response>;
@@ -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;AAInF,oBAAY,QAAQ,GAAG,IAAI,CAAC,iBAAiB,EAAE,MAAM,GAAG,IAAI,GAAG,QAAQ,CAAC,CAAC;AAEzE,oBAAY,UAAU,GAAG;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,EAAE,CAAC;CAChB,CAAC;AAGF,wBAAsB,eAAe,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,CAYtE;AAED,qBAAa,KAAK;IACJ,OAAO,CAAC,MAAM;gBAAN,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC;IAGtF,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;IAM5E,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;IAO9F,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO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OAAO,CAAC,MAAM,CAAC;IAOrD,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAO7C,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAMtD;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,CA4BpG;AAED,wBAAgB,OAAO,UAItB"}
package/out/cache.js CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.connect = exports.createFetchRmsRuntimeV2 = exports.getFetchRmsRuntimeV1 = exports.Cache = exports.getResponseBody = void 0;
4
- const tslib_1 = require("tslib");
5
4
  const https_1 = require("https");
6
- const node_fetch_1 = tslib_1.__importDefault(require("node-fetch"));
7
5
  const api_1 = require("@forge/api");
8
6
  async function getResponseBody(response) {
9
7
  const responseText = await response.text();
@@ -70,6 +68,21 @@ class Cache {
70
68
  const response = await this.client('rms/store/scan', this.buildRequest({ cursor, pattern, count }));
71
69
  return await getResponseBody(response);
72
70
  }
71
+ async leftPush(key, value) {
72
+ const response = await this.client('rms/store/lpush', this.buildRequest({ key, value }));
73
+ const { response: result } = await getResponseBody(response);
74
+ return result;
75
+ }
76
+ async rightPop(key) {
77
+ const response = await this.client('rms/store/rpop', this.buildRequest({ key }));
78
+ const { response: result } = await getResponseBody(response);
79
+ return result;
80
+ }
81
+ async listLength(key) {
82
+ const response = await this.client('rms/store/llen', this.buildRequest({ key }));
83
+ const { response: result } = await getResponseBody(response);
84
+ return result;
85
+ }
73
86
  }
74
87
  exports.Cache = Cache;
75
88
  function getFetchRmsRuntimeV1() {
@@ -85,9 +98,7 @@ function createFetchRmsRuntimeV2() {
85
98
  throw new Error('RMS config not available.');
86
99
  }
87
100
  const rmsFullUrl = new URL(rms.url) + path;
88
- const rmsAgent = new String('FORGE_PRODUCT_REQUEST');
89
- rmsAgent['passthrough'] = agent;
90
- return await (0, node_fetch_1.default)(rmsFullUrl, Object.assign(Object.assign({}, options), { agent: rmsAgent, headers: Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.headers), { Authorization: `Bearer ${proxy.token}`, Host: rms.host, 'x-b3-traceid': tracing.traceId, 'x-b3-spanid': tracing.spanId }) }));
101
+ 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, 'x-b3-traceid': tracing.traceId, 'x-b3-spanid': tracing.spanId }) }));
91
102
  };
92
103
  }
93
104
  exports.createFetchRmsRuntimeV2 = createFetchRmsRuntimeV2;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forge/cache",
3
- "version": "0.7.0-next.0",
3
+ "version": "0.7.0-next.2",
4
4
  "description": "Forge Cache methods",
5
5
  "author": "Atlassian",
6
6
  "license": "UNLICENSED",