@forge/cache 0.7.5-next.1 → 0.8.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/out/__test__/cache.test.js +18 -3
- package/out/cache.d.ts +6 -2
- package/out/cache.d.ts.map +1 -1
- package/out/cache.js +13 -22
- package/package.json +2 -2
|
@@ -56,6 +56,7 @@ describe('createFetch', () => {
|
|
|
56
56
|
await expect((0, cache_1.createFetchRmsRuntimeV2)()).rejects.toThrowError(new Error('RMS config not available.'));
|
|
57
57
|
});
|
|
58
58
|
it('creates a fetch that adds the right headers and url', async () => {
|
|
59
|
+
var _a;
|
|
59
60
|
global['__forge_runtime__'] = {
|
|
60
61
|
proxy: {
|
|
61
62
|
token: 'token',
|
|
@@ -72,10 +73,10 @@ describe('createFetch', () => {
|
|
|
72
73
|
};
|
|
73
74
|
const fetch = (0, cache_1.createFetchRmsRuntimeV2)();
|
|
74
75
|
await fetch('path');
|
|
75
|
-
const [absoluteUrl, options] = fetchMock.mock.lastCall
|
|
76
|
+
const [absoluteUrl, options] = (_a = fetchMock.mock.lastCall) !== null && _a !== void 0 ? _a : ['', {}];
|
|
76
77
|
expect(absoluteUrl.toString()).toEqual('https://rms/path');
|
|
77
|
-
expect((options
|
|
78
|
-
expect(options
|
|
78
|
+
expect((options === null || options === void 0 ? void 0 : options.agent)['passthrough']['keepAlive']).toBeTruthy();
|
|
79
|
+
expect(options === null || options === void 0 ? void 0 : options.headers).toMatchObject({
|
|
79
80
|
Authorization: 'Bearer token',
|
|
80
81
|
Host: 'rockmelon-storage.dev.atl-paas.net',
|
|
81
82
|
'x-b3-traceid': 'trace-id-test',
|
|
@@ -207,6 +208,13 @@ describe('Cache', () => {
|
|
|
207
208
|
expect(result).toEqual(0);
|
|
208
209
|
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
209
210
|
});
|
|
211
|
+
it('handles success with ttl', async () => {
|
|
212
|
+
const { cache, fetch } = buildCache();
|
|
213
|
+
fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 0 }), status: 200 });
|
|
214
|
+
const result = await cache.incrementAndGet('key', { ttlSeconds: 100 });
|
|
215
|
+
expect(result).toEqual(0);
|
|
216
|
+
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
217
|
+
});
|
|
210
218
|
it('handles success when key exists', async () => {
|
|
211
219
|
const { cache, fetch } = buildCache();
|
|
212
220
|
fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 11 }), status: 200 });
|
|
@@ -229,6 +237,13 @@ describe('Cache', () => {
|
|
|
229
237
|
expect(result).toEqual(0);
|
|
230
238
|
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
231
239
|
});
|
|
240
|
+
it('handles success with ttl', async () => {
|
|
241
|
+
const { cache, fetch } = buildCache();
|
|
242
|
+
fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 0 }), status: 200 });
|
|
243
|
+
const result = await cache.decrementAndGet('key', { ttlSeconds: 100 });
|
|
244
|
+
expect(result).toEqual(0);
|
|
245
|
+
expect(fetch.mock.lastCall).toMatchSnapshot();
|
|
246
|
+
});
|
|
232
247
|
it('handles success when key exists', async () => {
|
|
233
248
|
const { cache, fetch } = buildCache();
|
|
234
249
|
fetch.mockResolvedValueOnce({ ok: true, text: async () => JSON.stringify({ response: 11 }), status: 200 });
|
package/out/cache.d.ts
CHANGED
|
@@ -19,8 +19,12 @@ export declare class Cache {
|
|
|
19
19
|
getAndSet(key: string, value: string, opt?: {
|
|
20
20
|
ttlSeconds: number;
|
|
21
21
|
}): Promise<string | null>;
|
|
22
|
-
incrementAndGet(key: string
|
|
23
|
-
|
|
22
|
+
incrementAndGet(key: string, opt?: {
|
|
23
|
+
ttlSeconds: number;
|
|
24
|
+
}): Promise<number>;
|
|
25
|
+
decrementAndGet(key: string, opt?: {
|
|
26
|
+
ttlSeconds: number;
|
|
27
|
+
}): Promise<number>;
|
|
24
28
|
delete(key: string): Prom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OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAO3F,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE;QAAE,UAAU,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,MAAM,CAAC;IAO3E,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE;QAAE,UAAU,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,MAAM,CAAC;IAO3E,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAOpC,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,UAAU,CAAC;IAOrF,QAAQ,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,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,CAG/G;AAID,wBAAgB,uBAAuB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CA0BpG;AAED,wBAAgB,OAAO,UAItB"}
|
package/out/cache.js
CHANGED
|
@@ -19,7 +19,6 @@ async function getResponseBody(response) {
|
|
|
19
19
|
}
|
|
20
20
|
exports.getResponseBody = getResponseBody;
|
|
21
21
|
class Cache {
|
|
22
|
-
client;
|
|
23
22
|
constructor(client) {
|
|
24
23
|
this.client = client;
|
|
25
24
|
}
|
|
@@ -33,11 +32,11 @@ class Cache {
|
|
|
33
32
|
};
|
|
34
33
|
}
|
|
35
34
|
async set(key, value, opt) {
|
|
36
|
-
const response = await this.client('rms/store/set', this.buildRequest({ key, value,
|
|
35
|
+
const response = await this.client('rms/store/set', this.buildRequest(Object.assign({ key, value }, opt)));
|
|
37
36
|
await getResponseBody(response);
|
|
38
37
|
}
|
|
39
38
|
async setIfNotExists(key, value, opt) {
|
|
40
|
-
const response = await this.client('rms/store/setnx', this.buildRequest({ key, value,
|
|
39
|
+
const response = await this.client('rms/store/setnx', this.buildRequest(Object.assign({ key, value }, opt)));
|
|
41
40
|
const { response: result } = await getResponseBody(response);
|
|
42
41
|
return result;
|
|
43
42
|
}
|
|
@@ -47,17 +46,17 @@ class Cache {
|
|
|
47
46
|
return result;
|
|
48
47
|
}
|
|
49
48
|
async getAndSet(key, value, opt) {
|
|
50
|
-
const response = await this.client('rms/store/getset', this.buildRequest({ key, value,
|
|
49
|
+
const response = await this.client('rms/store/getset', this.buildRequest(Object.assign({ key, value }, opt)));
|
|
51
50
|
const { response: result } = await getResponseBody(response);
|
|
52
51
|
return result;
|
|
53
52
|
}
|
|
54
|
-
async incrementAndGet(key) {
|
|
55
|
-
const response = await this.client('rms/store/incr', this.buildRequest({ key }));
|
|
53
|
+
async incrementAndGet(key, opt) {
|
|
54
|
+
const response = await this.client('rms/store/incr', this.buildRequest(Object.assign({ key }, opt)));
|
|
56
55
|
const { response: result } = await getResponseBody(response);
|
|
57
56
|
return result;
|
|
58
57
|
}
|
|
59
|
-
async decrementAndGet(key) {
|
|
60
|
-
const response = await this.client('rms/store/decr', this.buildRequest({ key }));
|
|
58
|
+
async decrementAndGet(key, opt) {
|
|
59
|
+
const response = await this.client('rms/store/decr', this.buildRequest(Object.assign({ key }, opt)));
|
|
61
60
|
const { response: result } = await getResponseBody(response);
|
|
62
61
|
return result;
|
|
63
62
|
}
|
|
@@ -89,34 +88,26 @@ class Cache {
|
|
|
89
88
|
}
|
|
90
89
|
exports.Cache = Cache;
|
|
91
90
|
function getFetchRmsRuntimeV1() {
|
|
92
|
-
|
|
91
|
+
var _a;
|
|
92
|
+
return (_a = global.api) === null || _a === void 0 ? void 0 : _a.requestRmsStore;
|
|
93
93
|
}
|
|
94
94
|
exports.getFetchRmsRuntimeV1 = getFetchRmsRuntimeV1;
|
|
95
95
|
function createFetchRmsRuntimeV2() {
|
|
96
96
|
const agent = new https_1.Agent({ keepAlive: true });
|
|
97
97
|
return async function (path, options) {
|
|
98
98
|
const { proxy, rms, tracing } = (0, api_1.__getRuntime)();
|
|
99
|
-
if (!rms
|
|
99
|
+
if (!(rms === null || rms === void 0 ? void 0 : rms.url) || !(rms === null || rms === void 0 ? void 0 : rms.host)) {
|
|
100
100
|
throw new Error('RMS config not available.');
|
|
101
101
|
}
|
|
102
102
|
const rmsAgent = new String('FORGE_PRODUCT_REQUEST');
|
|
103
103
|
rmsAgent['passthrough'] = agent;
|
|
104
|
-
return await (0, node_fetch_1.default)(new URL(path, 'https://rms/'), {
|
|
105
|
-
...options,
|
|
106
|
-
agent: rmsAgent,
|
|
107
|
-
headers: {
|
|
108
|
-
...options?.headers,
|
|
109
|
-
Authorization: `Bearer ${proxy.token}`,
|
|
110
|
-
Host: rms.host,
|
|
111
|
-
'x-b3-traceid': tracing.traceId,
|
|
112
|
-
'x-b3-spanid': tracing.spanId
|
|
113
|
-
}
|
|
114
|
-
});
|
|
104
|
+
return await (0, node_fetch_1.default)(new URL(path, 'https://rms/'), 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 }) }));
|
|
115
105
|
};
|
|
116
106
|
}
|
|
117
107
|
exports.createFetchRmsRuntimeV2 = createFetchRmsRuntimeV2;
|
|
118
108
|
function connect() {
|
|
119
|
-
|
|
109
|
+
var _a;
|
|
110
|
+
const fetch = (_a = getFetchRmsRuntimeV1()) !== null && _a !== void 0 ? _a : createFetchRmsRuntimeV2();
|
|
120
111
|
return new Cache(fetch);
|
|
121
112
|
}
|
|
122
113
|
exports.connect = connect;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forge/cache",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0-next.0",
|
|
4
4
|
"description": "Forge Cache methods",
|
|
5
5
|
"author": "Atlassian",
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -20,6 +20,6 @@
|
|
|
20
20
|
"node-fetch": "2.7.0"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@forge/api": "^3.5.0
|
|
23
|
+
"@forge/api": "^3.5.0"
|
|
24
24
|
}
|
|
25
25
|
}
|