@forge/cache 0.1.0 → 0.2.0-next.1
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 +1 -3
- package/out/__test__/cache.test.js +46 -16
- package/out/cache.d.ts +4 -7
- package/out/cache.d.ts.map +1 -1
- package/out/cache.js +18 -21
- package/out/index.d.ts +5 -1
- package/out/index.d.ts.map +1 -1
- package/out/index.js +2 -3
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -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.
|
|
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.
|
|
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', () => {
|
|
@@ -136,12 +172,6 @@ describe('Cache', () => {
|
|
|
136
172
|
expect(result).toEqual(11);
|
|
137
173
|
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
138
174
|
});
|
|
139
|
-
it('handles success with ttl', async () => {
|
|
140
|
-
const { cache, fetch } = buildCache();
|
|
141
|
-
fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 13 }), status: 200 });
|
|
142
|
-
await cache.incrementAndGet('key', { ttlSeconds: 100 });
|
|
143
|
-
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
144
|
-
});
|
|
145
175
|
it('handles failure', async () => {
|
|
146
176
|
const { cache, fetch } = buildCache();
|
|
147
177
|
fetch.mockResolvedValueOnce({ ok: false, text: async () => 'Not allowed', status: 403 });
|
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(
|
|
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;
|
|
@@ -14,10 +12,9 @@ export declare class Cache {
|
|
|
14
12
|
ttlSeconds: number;
|
|
15
13
|
}): Promise<'OK' | null>;
|
|
16
14
|
get(key: string): Promise<string>;
|
|
17
|
-
incrementAndGet(key: string
|
|
18
|
-
ttlSeconds: number;
|
|
19
|
-
}): Promise<number>;
|
|
15
|
+
incrementAndGet(key: string): Promise<number>;
|
|
20
16
|
}
|
|
21
|
-
export declare function
|
|
17
|
+
export declare function getFetchRmsRuntimeV1(): ((path: string, options?: RequestInit) => Promise<Response>) | undefined;
|
|
18
|
+
export declare function createFetchRmsRuntimeV2(): (path: string, options?: RequestInit) => Promise<Response>;
|
|
22
19
|
export declare function connect(): Cache;
|
|
23
20
|
//# sourceMappingURL=cache.d.ts.map
|
package/out/cache.d.ts.map
CHANGED
|
@@ -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;
|
|
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;AAGzE,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,CAAC,MAAM,CAAC;IAOjC,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAM3D;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.
|
|
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(
|
|
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
|
|
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
|
|
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
|
|
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
|
-
async incrementAndGet(key
|
|
55
|
-
const
|
|
56
|
-
const response = await this.client('/rms/store/incr', this.buildRequest(Object.assign({ key }, opt)));
|
|
57
|
-
timer.stop();
|
|
46
|
+
async incrementAndGet(key) {
|
|
47
|
+
const response = await this.client('rms/store/incr', this.buildRequest({ key }));
|
|
58
48
|
const { response: result } = await getResponseBody(response);
|
|
59
49
|
return result;
|
|
60
50
|
}
|
|
61
51
|
}
|
|
62
52
|
exports.Cache = Cache;
|
|
63
|
-
function
|
|
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.
|
|
69
|
+
exports.createFetchRmsRuntimeV2 = createFetchRmsRuntimeV2;
|
|
75
70
|
function connect() {
|
|
76
|
-
|
|
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
package/out/index.d.ts.map
CHANGED
|
@@ -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
|
-
|
|
4
|
-
|
|
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.
|
|
3
|
+
"version": "0.2.0-next.1",
|
|
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"
|
|
24
|
-
"@forge/metrics": "^0.1.9"
|
|
23
|
+
"@forge/api": "^2.16.0"
|
|
25
24
|
}
|
|
26
25
|
}
|