@cfworker/cosmos 4.0.1 → 4.0.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/dist/commonjs/package.json +3 -0
- package/dist/{cjs → commonjs}/retry.js +2 -2
- package/dist/{types → commonjs}/util.d.ts +1 -1
- package/dist/esm/client.d.ts +144 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/response.d.ts +24 -0
- package/dist/esm/retry.d.ts +21 -0
- package/dist/esm/retry.js +1 -1
- package/dist/esm/session.d.ts +24 -0
- package/dist/esm/signer.d.ts +12 -0
- package/dist/esm/types.d.ts +62 -0
- package/dist/esm/util.d.ts +5 -0
- package/package.json +32 -27
- package/dist/tsconfig.cjs.tsbuildinfo +0 -1
- package/dist/tsconfig.tsbuildinfo +0 -1
- package/src/client.ts +0 -581
- package/src/response.ts +0 -59
- package/src/retry.ts +0 -45
- package/src/session.ts +0 -74
- package/src/signer.ts +0 -63
- package/src/types.ts +0 -82
- package/src/util.ts +0 -59
- /package/dist/{types → commonjs}/client.d.ts +0 -0
- /package/dist/{cjs → commonjs}/client.js +0 -0
- /package/dist/{types → commonjs}/index.d.ts +0 -0
- /package/dist/{cjs → commonjs}/index.js +0 -0
- /package/dist/{types → commonjs}/response.d.ts +0 -0
- /package/dist/{cjs → commonjs}/response.js +0 -0
- /package/dist/{types → commonjs}/retry.d.ts +0 -0
- /package/dist/{types → commonjs}/session.d.ts +0 -0
- /package/dist/{cjs → commonjs}/session.js +0 -0
- /package/dist/{types → commonjs}/signer.d.ts +0 -0
- /package/dist/{cjs → commonjs}/signer.js +0 -0
- /package/dist/{types → commonjs}/types.d.ts +0 -0
- /package/dist/{cjs → commonjs}/types.js +0 -0
- /package/dist/{cjs → commonjs}/util.js +0 -0
- /package/{src/index.ts → dist/esm/index.d.ts} +0 -0
package/src/session.ts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
const readSessionNotAvailableSubstatus: string = '1002';
|
|
2
|
-
|
|
3
|
-
export function readSessionNotAvailable(response: Response): boolean {
|
|
4
|
-
return (
|
|
5
|
-
response.status === 404 &&
|
|
6
|
-
response.headers.get('x-ms-substatus') === readSessionNotAvailableSubstatus
|
|
7
|
-
);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Get the database and collection id from the x-ms-alt-content-path header or url.
|
|
12
|
-
* @param requestOrResponse
|
|
13
|
-
*/
|
|
14
|
-
export function getCollectionId({
|
|
15
|
-
url,
|
|
16
|
-
headers
|
|
17
|
-
}: {
|
|
18
|
-
url: string;
|
|
19
|
-
headers: Headers;
|
|
20
|
-
}): { dbId: string; collId: string } | null {
|
|
21
|
-
const path =
|
|
22
|
-
headers.get('x-ms-alt-content-path') ?? new URL(url).pathname.substr(1);
|
|
23
|
-
const [, dbId, colls, collId] = path.split('/');
|
|
24
|
-
if (colls !== 'colls' || !collId) {
|
|
25
|
-
return null;
|
|
26
|
-
}
|
|
27
|
-
return { dbId, collId };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface SessionTokens {
|
|
31
|
-
[dbId: string]: { [collId: string]: string };
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface SessionContainer {
|
|
35
|
-
tokens: SessionTokens;
|
|
36
|
-
setRequestSession(request: Request): void;
|
|
37
|
-
readResponseSession(response: Response): void;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export class DefaultSessionContainer implements SessionContainer {
|
|
41
|
-
constructor(public readonly tokens: SessionTokens = {}) {}
|
|
42
|
-
|
|
43
|
-
setRequestSession(request: Request): void {
|
|
44
|
-
if (request.headers.get('x-ms-consistency-level') !== 'Session') {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
const ids = getCollectionId(request);
|
|
48
|
-
if (!ids) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
const token = this.tokens?.[ids.dbId]?.[ids.collId];
|
|
52
|
-
if (token) {
|
|
53
|
-
request.headers.set('x-ms-session-token', token);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
readResponseSession(response: Response): void {
|
|
58
|
-
const ids = getCollectionId(response);
|
|
59
|
-
if (!ids) {
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
if (readSessionNotAvailable(response)) {
|
|
63
|
-
if (this.tokens[ids.dbId]) {
|
|
64
|
-
delete this.tokens[ids.dbId][ids.collId];
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
const token = response.headers.get('x-ms-session-token');
|
|
68
|
-
if (!token) {
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
this.tokens[ids.dbId] ??= {};
|
|
72
|
-
this.tokens[ids.dbId][ids.collId] = token;
|
|
73
|
-
}
|
|
74
|
-
}
|
package/src/signer.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { base64 } from 'rfc4648';
|
|
2
|
-
|
|
3
|
-
export class Signer {
|
|
4
|
-
private readonly masterkey: Promise<CryptoKey>;
|
|
5
|
-
private readonly encoder = new TextEncoder();
|
|
6
|
-
|
|
7
|
-
constructor(masterkey: string) {
|
|
8
|
-
const promiseLike = crypto.subtle.importKey(
|
|
9
|
-
'raw',
|
|
10
|
-
base64.parse(masterkey),
|
|
11
|
-
{ name: 'HMAC', hash: { name: 'SHA-256' } },
|
|
12
|
-
false,
|
|
13
|
-
['sign']
|
|
14
|
-
);
|
|
15
|
-
this.masterkey = Promise.resolve(promiseLike);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public async sign(request: Request, date: Date = new Date()): Promise<void> {
|
|
19
|
-
const key = await this.masterkey;
|
|
20
|
-
const payload = this.getPayload(request, date);
|
|
21
|
-
const hashed = await crypto.subtle.sign('HMAC', key, payload);
|
|
22
|
-
const signature = base64.stringify(new Uint8Array(hashed));
|
|
23
|
-
request.headers.set(
|
|
24
|
-
'authorization',
|
|
25
|
-
encodeURIComponent(`type=master&ver=1.0&sig=${signature}`)
|
|
26
|
-
);
|
|
27
|
-
request.headers.set('x-ms-date', date.toUTCString());
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public getPayload(request: Request, date: Date): Uint8Array {
|
|
31
|
-
const url = new URL(request.url);
|
|
32
|
-
const { resourceType, resourceId } = this.pathnameToResource(url.pathname);
|
|
33
|
-
const method = request.method.toLowerCase();
|
|
34
|
-
const text = `${method}\n${resourceType}\n${resourceId}\n${date
|
|
35
|
-
.toUTCString()
|
|
36
|
-
.toLowerCase()}\n\n`;
|
|
37
|
-
return this.encoder.encode(text);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public pathnameToResource(pathname: string): {
|
|
41
|
-
resourceType: string;
|
|
42
|
-
resourceId: string;
|
|
43
|
-
} {
|
|
44
|
-
const segments = pathname.split('/');
|
|
45
|
-
if (segments.length % 2 === 0) {
|
|
46
|
-
const resourceType = segments[segments.length - 1];
|
|
47
|
-
const resourceId = segments.slice(1, segments.length - 1).join('/');
|
|
48
|
-
return { resourceType, resourceId };
|
|
49
|
-
}
|
|
50
|
-
const resourceType = segments[segments.length - 2];
|
|
51
|
-
const resourceId = segments.slice(1, segments.length).join('/');
|
|
52
|
-
return { resourceType, resourceId };
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const signers: Record<string, Signer> = {};
|
|
57
|
-
|
|
58
|
-
export function getSigner(masterKey: string): Signer {
|
|
59
|
-
if (!signers[masterKey]) {
|
|
60
|
-
signers[masterKey] = new Signer(masterKey);
|
|
61
|
-
}
|
|
62
|
-
return signers[masterKey];
|
|
63
|
-
}
|
package/src/types.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
export type ConsistencyLevel = 'Strong' | 'Bounded' | 'Session' | 'Eventual';
|
|
2
|
-
|
|
3
|
-
export type OfferType = 'S1' | 'S2' | 'S3';
|
|
4
|
-
|
|
5
|
-
export type IndexingDirective = 'Include' | 'Exclude';
|
|
6
|
-
|
|
7
|
-
export type IndexingMode = 'none' | 'consistent' | 'lazy'; // lazy is deemphasized https://github.com/MicrosoftDocs/azure-docs/issues/23875
|
|
8
|
-
|
|
9
|
-
export interface Resource {
|
|
10
|
-
id: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface PersistedResource extends Resource {
|
|
14
|
-
_rid: string;
|
|
15
|
-
_self: string;
|
|
16
|
-
_etag: string;
|
|
17
|
-
_ts: number;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface Document extends PersistedResource {
|
|
21
|
-
_attachments: 'attachments/';
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface Database extends PersistedResource {
|
|
25
|
-
_colls: 'colls/';
|
|
26
|
-
_users: 'users/';
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export interface IndexingPolicy {
|
|
30
|
-
indexingMode: IndexingMode;
|
|
31
|
-
automatic: boolean;
|
|
32
|
-
includedPaths?: {
|
|
33
|
-
path: string;
|
|
34
|
-
indexes?: {
|
|
35
|
-
dataType: 'String' | 'Number';
|
|
36
|
-
precision: number;
|
|
37
|
-
kind: 'Range' | 'Hash';
|
|
38
|
-
}[];
|
|
39
|
-
}[];
|
|
40
|
-
excludedPaths?: { path: string }[];
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export interface PartitionKeyDefinition {
|
|
44
|
-
paths: string[];
|
|
45
|
-
kind: 'Hash';
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export interface ConflictResolutionPolicy {
|
|
49
|
-
mode: 'LastWriterWins';
|
|
50
|
-
conflictResolutionPath: '/_ts';
|
|
51
|
-
conflictResolutionProcedure: '';
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export interface GeospatialConfig {
|
|
55
|
-
type: 'Geography';
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export interface Collection extends PersistedResource {
|
|
59
|
-
indexingPolicy: IndexingPolicy;
|
|
60
|
-
partitionKey: PartitionKeyDefinition;
|
|
61
|
-
conflictResolutionPolicy: ConflictResolutionPolicy;
|
|
62
|
-
geospatialConfig: GeospatialConfig;
|
|
63
|
-
_docs: 'docs/';
|
|
64
|
-
_sprocs: 'sprocs/';
|
|
65
|
-
_triggers: 'triggers/';
|
|
66
|
-
_udfs: 'udfs/';
|
|
67
|
-
_conflicts: 'conflicts/';
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export interface ConnectionInfo {
|
|
71
|
-
/**
|
|
72
|
-
* Cosmos DB endpoint.
|
|
73
|
-
* @example
|
|
74
|
-
* "https://xxxxxxxxxx.documents.azure.com"
|
|
75
|
-
*/
|
|
76
|
-
endpoint: string;
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Cosmos DB key.
|
|
80
|
-
*/
|
|
81
|
-
masterKey: string;
|
|
82
|
-
}
|
package/src/util.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { ConnectionInfo } from './types';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Escape non-ASCII characters as \uXXXX
|
|
5
|
-
*/
|
|
6
|
-
export function escapeNonASCII(s: string): string {
|
|
7
|
-
return s.replace(
|
|
8
|
-
/[\u0080-\uFFFF]/g,
|
|
9
|
-
m => '\\u' + m.charCodeAt(0).toString(16).padStart(4, '0')
|
|
10
|
-
);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Asserts argument is truthy.
|
|
15
|
-
* @param name Argument name.
|
|
16
|
-
* @param value Argument value.
|
|
17
|
-
*/
|
|
18
|
-
export function assertArg(name: string, value: any): asserts value {
|
|
19
|
-
if (!value) {
|
|
20
|
-
throw new Error(`${name} is required.`);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Template literal tag which URI encodes values.
|
|
26
|
-
*/
|
|
27
|
-
export function uri(strings: TemplateStringsArray, ...values: any[]): string {
|
|
28
|
-
let s = strings[0];
|
|
29
|
-
for (let i = 0; i < values.length; i++) {
|
|
30
|
-
s += encodeURIComponent(values[i]) + strings[i + 1];
|
|
31
|
-
}
|
|
32
|
-
return s;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Parses a connection string for Cosmos DB.
|
|
37
|
-
* @param connString Cosmos DB connection string.
|
|
38
|
-
*/
|
|
39
|
-
export function parseConnectionString(connString: string): ConnectionInfo {
|
|
40
|
-
const props: Record<string, string> = {};
|
|
41
|
-
connString.split(';').forEach(kv => {
|
|
42
|
-
const pos = kv.indexOf('=');
|
|
43
|
-
if (pos < 1) {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
const key = kv.slice(0, pos);
|
|
47
|
-
const val = kv.slice(pos + 1);
|
|
48
|
-
if (key && val) {
|
|
49
|
-
props[key] = val;
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
if (!props.AccountEndpoint || !props.AccountKey) {
|
|
53
|
-
throw new Error('Invalid connection string');
|
|
54
|
-
}
|
|
55
|
-
return {
|
|
56
|
-
endpoint: props.AccountEndpoint,
|
|
57
|
-
masterKey: props.AccountKey
|
|
58
|
-
};
|
|
59
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|