@cfworker/cosmos 1.13.0 → 1.13.3
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/dist/client.js +3 -3
- package/dist/signer.d.ts +1 -1
- package/dist/signer.js +7 -7
- package/dist/types.d.ts +1 -1
- package/dist/util.js +1 -1
- package/package.json +2 -2
- package/src/client.ts +4 -4
- package/src/signer.ts +7 -10
- package/src/types.ts +1 -1
- package/src/util.ts +1 -1
package/dist/client.js
CHANGED
|
@@ -7,14 +7,14 @@ export class CosmosClient {
|
|
|
7
7
|
constructor(config) {
|
|
8
8
|
this.requestCharges = 0;
|
|
9
9
|
this.retries = { count: 0, delayMs: 0 };
|
|
10
|
-
let { endpoint,
|
|
10
|
+
let { endpoint, masterKey } = 'connectionString' in config
|
|
11
11
|
? parseConnectionString(config.connectionString)
|
|
12
12
|
: config;
|
|
13
13
|
if (endpoint.endsWith('/')) {
|
|
14
14
|
endpoint = endpoint.slice(0, -1);
|
|
15
15
|
}
|
|
16
16
|
this.endpoint = endpoint;
|
|
17
|
-
this.signer = getSigner(
|
|
17
|
+
this.signer = getSigner(masterKey);
|
|
18
18
|
this.retryPolicy = config.retryPolicy ?? defaultRetryPolicy;
|
|
19
19
|
this.consistencyLevel = config.consistencyLevel ?? 'Session';
|
|
20
20
|
this.dbId = config.dbId;
|
|
@@ -180,7 +180,7 @@ export class CosmosClient {
|
|
|
180
180
|
};
|
|
181
181
|
}
|
|
182
182
|
async fetchWithRetry(request, context) {
|
|
183
|
-
const retryRequest = request.
|
|
183
|
+
const retryRequest = new Request(request.url, request);
|
|
184
184
|
const response = await this.fetch(request);
|
|
185
185
|
context ??= { attempts: 0, cumulativeWaitMs: 0, request, response };
|
|
186
186
|
context.attempts++;
|
package/dist/signer.d.ts
CHANGED
package/dist/signer.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { base64 } from 'rfc4648';
|
|
2
2
|
export class Signer {
|
|
3
3
|
constructor(masterkey) {
|
|
4
4
|
this.encoder = new TextEncoder();
|
|
5
|
-
const promiseLike = crypto.subtle.importKey('raw',
|
|
5
|
+
const promiseLike = crypto.subtle.importKey('raw', base64.parse(masterkey), { name: 'HMAC', hash: { name: 'SHA-256' } }, false, ['sign']);
|
|
6
6
|
this.masterkey = Promise.resolve(promiseLike);
|
|
7
7
|
}
|
|
8
8
|
async sign(request, date = new Date()) {
|
|
9
9
|
const key = await this.masterkey;
|
|
10
10
|
const payload = this.getPayload(request, date);
|
|
11
11
|
const hashed = await crypto.subtle.sign('HMAC', key, payload);
|
|
12
|
-
const signature =
|
|
12
|
+
const signature = base64.stringify(new Uint8Array(hashed));
|
|
13
13
|
request.headers.set('authorization', encodeURIComponent(`type=master&ver=1.0&sig=${signature}`));
|
|
14
14
|
request.headers.set('x-ms-date', date.toUTCString());
|
|
15
15
|
}
|
|
@@ -35,9 +35,9 @@ export class Signer {
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
const signers = {};
|
|
38
|
-
export function getSigner(
|
|
39
|
-
if (!signers[
|
|
40
|
-
signers[
|
|
38
|
+
export function getSigner(masterKey) {
|
|
39
|
+
if (!signers[masterKey]) {
|
|
40
|
+
signers[masterKey] = new Signer(masterKey);
|
|
41
41
|
}
|
|
42
|
-
return signers[
|
|
42
|
+
return signers[masterKey];
|
|
43
43
|
}
|
package/dist/types.d.ts
CHANGED
package/dist/util.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cfworker/cosmos",
|
|
3
|
-
"version": "1.13.
|
|
3
|
+
"version": "1.13.3",
|
|
4
4
|
"description": "Azure Cosmos DB client for Cloudflare Workers and service workers",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"azure",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@cfworker/worker-types": "^1.12.1",
|
|
41
41
|
"@cloudflare/workers-types": "^2.2.2",
|
|
42
|
-
"
|
|
42
|
+
"rfc4648": "^1.5.1"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@cfworker/dev": "^1.14.0",
|
package/src/client.ts
CHANGED
|
@@ -27,7 +27,7 @@ export type CosmosClientConfig =
|
|
|
27
27
|
/**
|
|
28
28
|
* Cosmos DB connection string.
|
|
29
29
|
* @example
|
|
30
|
-
* "AccountEndpoint=https://xxxxxxxxxx.documents.azure.com:443/;
|
|
30
|
+
* "AccountEndpoint=https://xxxxxxxxxx.documents.azure.com:443/;AccountKey=xxxxxxxxxx;"
|
|
31
31
|
*/
|
|
32
32
|
connectionString: string;
|
|
33
33
|
} & CosmosClientConfigBase);
|
|
@@ -84,7 +84,7 @@ export class CosmosClient {
|
|
|
84
84
|
public retries = { count: 0, delayMs: 0 };
|
|
85
85
|
|
|
86
86
|
constructor(config: CosmosClientConfig) {
|
|
87
|
-
let { endpoint,
|
|
87
|
+
let { endpoint, masterKey } =
|
|
88
88
|
'connectionString' in config
|
|
89
89
|
? parseConnectionString(config.connectionString)
|
|
90
90
|
: config;
|
|
@@ -92,7 +92,7 @@ export class CosmosClient {
|
|
|
92
92
|
endpoint = endpoint.slice(0, -1);
|
|
93
93
|
}
|
|
94
94
|
this.endpoint = endpoint;
|
|
95
|
-
this.signer = getSigner(
|
|
95
|
+
this.signer = getSigner(masterKey);
|
|
96
96
|
this.retryPolicy = config.retryPolicy ?? defaultRetryPolicy;
|
|
97
97
|
this.consistencyLevel = config.consistencyLevel ?? 'Session';
|
|
98
98
|
this.dbId = config.dbId;
|
|
@@ -330,7 +330,7 @@ export class CosmosClient {
|
|
|
330
330
|
request: Request,
|
|
331
331
|
context?: RetryContext
|
|
332
332
|
): Promise<Response> {
|
|
333
|
-
const retryRequest = request.
|
|
333
|
+
const retryRequest = new Request(request.url, request);
|
|
334
334
|
|
|
335
335
|
const response = await this.fetch(request);
|
|
336
336
|
|
package/src/signer.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
decode as base64ToArrayBuffer,
|
|
3
|
-
encode as arrayBufferToBase64
|
|
4
|
-
} from 'base64-arraybuffer';
|
|
1
|
+
import { base64 } from 'rfc4648';
|
|
5
2
|
|
|
6
3
|
export class Signer {
|
|
7
4
|
private readonly masterkey: Promise<CryptoKey>;
|
|
@@ -10,7 +7,7 @@ export class Signer {
|
|
|
10
7
|
constructor(masterkey: string) {
|
|
11
8
|
const promiseLike = crypto.subtle.importKey(
|
|
12
9
|
'raw',
|
|
13
|
-
|
|
10
|
+
base64.parse(masterkey),
|
|
14
11
|
{ name: 'HMAC', hash: { name: 'SHA-256' } },
|
|
15
12
|
false,
|
|
16
13
|
['sign']
|
|
@@ -22,7 +19,7 @@ export class Signer {
|
|
|
22
19
|
const key = await this.masterkey;
|
|
23
20
|
const payload = this.getPayload(request, date);
|
|
24
21
|
const hashed = await crypto.subtle.sign('HMAC', key, payload);
|
|
25
|
-
const signature =
|
|
22
|
+
const signature = base64.stringify(new Uint8Array(hashed));
|
|
26
23
|
request.headers.set(
|
|
27
24
|
'authorization',
|
|
28
25
|
encodeURIComponent(`type=master&ver=1.0&sig=${signature}`)
|
|
@@ -55,9 +52,9 @@ export class Signer {
|
|
|
55
52
|
|
|
56
53
|
const signers: Record<string, Signer> = {};
|
|
57
54
|
|
|
58
|
-
export function getSigner(
|
|
59
|
-
if (!signers[
|
|
60
|
-
signers[
|
|
55
|
+
export function getSigner(masterKey: string) {
|
|
56
|
+
if (!signers[masterKey]) {
|
|
57
|
+
signers[masterKey] = new Signer(masterKey);
|
|
61
58
|
}
|
|
62
|
-
return signers[
|
|
59
|
+
return signers[masterKey];
|
|
63
60
|
}
|
package/src/types.ts
CHANGED