nodestack-client 2.0.0 → 2.1.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/README.md +52 -0
- package/dist/Client.d.ts +1 -0
- package/dist/Client.d.ts.map +1 -1
- package/dist/auth/PkceUtil.d.ts +9 -0
- package/dist/auth/PkceUtil.d.ts.map +1 -0
- package/dist/index.cjs +261 -1
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +261 -1
- package/dist/index.mjs.map +3 -3
- package/dist/services/RecordService.d.ts +18 -1
- package/dist/services/RecordService.d.ts.map +1 -1
- package/dist/types.d.ts +64 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +50 -50
package/README.md
CHANGED
|
@@ -68,6 +68,58 @@ console.log('JWT Token:', client.authStore.token);
|
|
|
68
68
|
console.log('Is valid:', client.authStore.isValid);
|
|
69
69
|
```
|
|
70
70
|
|
|
71
|
+
### Authenticate via OAuth2 / Social Login (PKCE)
|
|
72
|
+
NodeStack supports native PKCE code exchange for Google, GitHub, Apple, Discord, and Microsoft:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
// 1. Fetch available providers and precomputed PKCE challenge & state
|
|
76
|
+
const { authProviders } = await client.collection('users').listAuthMethods('http://localhost:3000/callback');
|
|
77
|
+
const google = authProviders.find((p) => p.name === 'google');
|
|
78
|
+
|
|
79
|
+
// 2. Redirect user or open popup with google.authUrl
|
|
80
|
+
// window.location.href = google.authUrl;
|
|
81
|
+
|
|
82
|
+
// 3. On your callback page (/callback), exchange code and codeVerifier
|
|
83
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
84
|
+
const code = urlParams.get('code');
|
|
85
|
+
|
|
86
|
+
const authData = await client.collection('users').authWithOAuth2({
|
|
87
|
+
provider: 'google',
|
|
88
|
+
code: code!,
|
|
89
|
+
codeVerifier: google.codeVerifier,
|
|
90
|
+
state: google.state,
|
|
91
|
+
redirectUrl: 'http://localhost:3000/callback',
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
console.log('Logged in via Google:', authData.record);
|
|
95
|
+
console.log('Is newly registered user:', authData.meta.isNew);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
#### One-Step Popup Authentication (Single-Page Apps)
|
|
99
|
+
```typescript
|
|
100
|
+
// nodestack-client verifies popup origin, window source, and binds CSRF state automatically:
|
|
101
|
+
const authData = await client.collection('users').authWithOAuth2({
|
|
102
|
+
provider: 'google',
|
|
103
|
+
urlCallback: (url) => window.open(url, '_blank', 'width=500,height=650'),
|
|
104
|
+
redirectUrl: 'http://localhost:3000/callback',
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Machine-to-Machine (M2M) Authentication with API Keys
|
|
109
|
+
For background workers, cron daemons, serverless scripts, and CI/CD pipelines:
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
import { NodeStackClient } from 'nodestack-client';
|
|
113
|
+
|
|
114
|
+
// Initialized with a scoped API key (e.g. "orders:*", "products:read")
|
|
115
|
+
const client = new NodeStackClient('https://api.yourdomain.com', {
|
|
116
|
+
apiKey: process.env.NODESTACK_API_KEY, // 'nsk_live_...'
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// Requests automatically inject Authorization: Bearer nsk_...
|
|
120
|
+
const orders = await client.collection('orders').getList(1, 20);
|
|
121
|
+
```
|
|
122
|
+
|
|
71
123
|
### Authenticate as a Superuser / Admin
|
|
72
124
|
```typescript
|
|
73
125
|
const adminAuth = await client.admins.authWithPassword('admin@nodestack.io', 'MySecretAdminPassword');
|
package/dist/Client.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ import type { ClientOptions, SendOptions, RecordModel, ChaosOptions } from './ty
|
|
|
15
15
|
export declare class NodeStackClient<TCollections extends Record<string, any> = Record<string, RecordModel>> {
|
|
16
16
|
readonly baseUrl: string;
|
|
17
17
|
readonly authStore: BaseAuthStore;
|
|
18
|
+
apiKey?: string;
|
|
18
19
|
readonly files: FileService;
|
|
19
20
|
readonly realtime: RealtimeService;
|
|
20
21
|
readonly admins: AdminService;
|
package/dist/Client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Client.d.ts","sourceRoot":"","sources":["../src/Client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAGrD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,KAAK,EACV,aAAa,EACb,WAAW,EACX,WAAW,EACX,YAAY,EACb,MAAM,SAAS,CAAC;AAEjB;;GAEG;AACH,qBAAa,eAAe,CAAC,YAAY,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC;IACjG,SAAgB,OAAO,EAAE,MAAM,CAAC;IAChC,SAAgB,SAAS,EAAE,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"Client.d.ts","sourceRoot":"","sources":["../src/Client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAGrD,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,WAAW,E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ments.
|
|
3
|
+
*/
|
|
4
|
+
export declare class PkceUtil {
|
|
5
|
+
static generateVerifier(length?: number): string;
|
|
6
|
+
static generateChallenge(verifier: string): Promise<string>;
|
|
7
|
+
static base64UrlEncode(bytes: Uint8Array): string;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=PkceUtil.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PkceUtil.d.ts","sourceRoot":"","sources":["../../src/auth/PkceUtil.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,QAAQ;IACnB,OAAc,gBAAgB,CAAC,MAAM,SAAK,GAAG,MAAM,CAgClD;IAED,OAAoB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAsBvE;IAED,OAAc,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAiCvD;CACF"}
|
package/dist/index.cjs
CHANGED
|
@@ -31,6 +31,7 @@ __export(index_exports, {
|
|
|
31
31
|
LocalAuthStore: () => LocalAuthStore,
|
|
32
32
|
MemoryAuthStore: () => MemoryAuthStore,
|
|
33
33
|
NodeStackClient: () => NodeStackClient,
|
|
34
|
+
PkceUtil: () => PkceUtil,
|
|
34
35
|
RealtimeService: () => RealtimeService,
|
|
35
36
|
RecordService: () => RecordService,
|
|
36
37
|
SnapshotService: () => SnapshotService,
|
|
@@ -377,6 +378,175 @@ var RecordService = class extends BaseService {
|
|
|
377
378
|
}
|
|
378
379
|
return result;
|
|
379
380
|
}
|
|
381
|
+
/**
|
|
382
|
+
* Returns a list of configured and enabled OAuth2 authentication providers.
|
|
383
|
+
*/
|
|
384
|
+
async listAuthMethods(redirectUri) {
|
|
385
|
+
return this.send(
|
|
386
|
+
`/api/collections/${encodeURIComponent(this.collectionName)}/auth-methods`,
|
|
387
|
+
{
|
|
388
|
+
method: "GET",
|
|
389
|
+
query: redirectUri ? { redirectUri } : void 0
|
|
390
|
+
}
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Authenticates against an auth collection using OAuth2 with PKCE code exchange.
|
|
395
|
+
* On success, automatically persists the token and record to authStore.
|
|
396
|
+
*/
|
|
397
|
+
async authWithOAuth2(options) {
|
|
398
|
+
let code = options.code;
|
|
399
|
+
let codeVerifier = options.codeVerifier;
|
|
400
|
+
if ((options.urlCallback || options.popup || typeof window !== "undefined" && !code && !codeVerifier) && (!code || !codeVerifier)) {
|
|
401
|
+
const authMethods = await this.listAuthMethods(options.redirectUrl);
|
|
402
|
+
const provider = authMethods.authProviders.find(
|
|
403
|
+
(p) => p.name.toLowerCase() === options.provider.toLowerCase()
|
|
404
|
+
);
|
|
405
|
+
if (!provider) {
|
|
406
|
+
throw new Error(`OAuth provider '${options.provider}' is not enabled or configured.`);
|
|
407
|
+
}
|
|
408
|
+
codeVerifier = provider.codeVerifier;
|
|
409
|
+
const expectedState = provider.state;
|
|
410
|
+
let expectedOrigin = null;
|
|
411
|
+
if (typeof window !== "undefined" && window.location) {
|
|
412
|
+
if (options.redirectUrl) {
|
|
413
|
+
try {
|
|
414
|
+
expectedOrigin = new URL(options.redirectUrl, window.location.href).origin;
|
|
415
|
+
} catch {
|
|
416
|
+
expectedOrigin = window.location.origin;
|
|
417
|
+
}
|
|
418
|
+
} else {
|
|
419
|
+
expectedOrigin = window.location.origin;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
let popup = options.popup || null;
|
|
423
|
+
if (options.urlCallback) {
|
|
424
|
+
const cbResult = options.urlCallback(provider.authUrl);
|
|
425
|
+
if (!popup && cbResult && typeof cbResult.postMessage === "function") {
|
|
426
|
+
popup = cbResult;
|
|
427
|
+
}
|
|
428
|
+
} else if (typeof window !== "undefined" && typeof window.open === "function") {
|
|
429
|
+
popup = window.open(provider.authUrl, "_blank", "width=600,height=700");
|
|
430
|
+
if (!popup) {
|
|
431
|
+
throw new Error("Failed to open OAuth popup window. It may have been blocked by the browser.");
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
return new Promise((resolve, reject) => {
|
|
435
|
+
if (typeof window !== "undefined" && window.addEventListener) {
|
|
436
|
+
let pollTimer = null;
|
|
437
|
+
const cleanup = () => {
|
|
438
|
+
if (pollTimer) {
|
|
439
|
+
clearInterval(pollTimer);
|
|
440
|
+
pollTimer = null;
|
|
441
|
+
}
|
|
442
|
+
if (typeof window !== "undefined" && window.removeEventListener) {
|
|
443
|
+
window.removeEventListener("message", handler);
|
|
444
|
+
}
|
|
445
|
+
};
|
|
446
|
+
const handler = async (event) => {
|
|
447
|
+
if (!event.data || typeof event.data !== "object" || event.data.type !== "nodestack_oauth_callback") {
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (expectedOrigin && event.origin !== expectedOrigin) {
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
if (popup) {
|
|
454
|
+
if (event.source !== popup) {
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
} else if (event.source === window) {
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
if (expectedState && event.data.state !== expectedState) {
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
cleanup();
|
|
464
|
+
if (popup && typeof popup.close === "function") {
|
|
465
|
+
try {
|
|
466
|
+
popup.close();
|
|
467
|
+
} catch {
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
if (event.data.error) {
|
|
471
|
+
const errMsg = typeof event.data.error === "string" ? event.data.error : event.data.error.message || event.data.error_description || JSON.stringify(event.data.error);
|
|
472
|
+
return reject(new Error(errMsg));
|
|
473
|
+
}
|
|
474
|
+
if (!event.data.code) {
|
|
475
|
+
return reject(new Error("OAuth callback message is missing authorization code."));
|
|
476
|
+
}
|
|
477
|
+
try {
|
|
478
|
+
const res = await this.authWithOAuth2({
|
|
479
|
+
provider: options.provider,
|
|
480
|
+
code: event.data.code,
|
|
481
|
+
codeVerifier,
|
|
482
|
+
redirectUrl: options.redirectUrl,
|
|
483
|
+
createData: options.createData,
|
|
484
|
+
state: expectedState
|
|
485
|
+
});
|
|
486
|
+
resolve(res);
|
|
487
|
+
} catch (err) {
|
|
488
|
+
reject(err);
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
window.addEventListener("message", handler);
|
|
492
|
+
if (popup) {
|
|
493
|
+
pollTimer = setInterval(() => {
|
|
494
|
+
if (popup && popup.closed) {
|
|
495
|
+
cleanup();
|
|
496
|
+
reject(new Error("The OAuth popup window was closed before completing authentication."));
|
|
497
|
+
}
|
|
498
|
+
}, 500);
|
|
499
|
+
}
|
|
500
|
+
} else {
|
|
501
|
+
reject(new Error("urlCallback requires code and codeVerifier in non-browser environments."));
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
if (!code || !codeVerifier) {
|
|
506
|
+
throw new Error("Both code and codeVerifier are required for OAuth2 authentication");
|
|
507
|
+
}
|
|
508
|
+
const result = await this.send(
|
|
509
|
+
`/api/collections/${encodeURIComponent(this.collectionName)}/auth-with-oauth2`,
|
|
510
|
+
{
|
|
511
|
+
method: "POST",
|
|
512
|
+
body: {
|
|
513
|
+
provider: options.provider,
|
|
514
|
+
code,
|
|
515
|
+
codeVerifier,
|
|
516
|
+
state: options.state,
|
|
517
|
+
redirectUrl: options.redirectUrl,
|
|
518
|
+
createData: options.createData
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
);
|
|
522
|
+
if (result && result.token) {
|
|
523
|
+
this.client.authStore.save(result.token, result.record);
|
|
524
|
+
}
|
|
525
|
+
return result;
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Lists external social identities linked to a specific record.
|
|
529
|
+
*/
|
|
530
|
+
async listExternalAuths(recordId) {
|
|
531
|
+
return this.send(
|
|
532
|
+
`/api/collections/${encodeURIComponent(this.collectionName)}/records/${encodeURIComponent(recordId)}/external-auths`,
|
|
533
|
+
{
|
|
534
|
+
method: "GET"
|
|
535
|
+
}
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* Unlinks an external social identity from a record.
|
|
540
|
+
*/
|
|
541
|
+
async unlinkExternalAuth(recordId, provider) {
|
|
542
|
+
await this.send(
|
|
543
|
+
`/api/collections/${encodeURIComponent(this.collectionName)}/records/${encodeURIComponent(recordId)}/external-auths/${encodeURIComponent(provider)}`,
|
|
544
|
+
{
|
|
545
|
+
method: "DELETE"
|
|
546
|
+
}
|
|
547
|
+
);
|
|
548
|
+
return true;
|
|
549
|
+
}
|
|
380
550
|
/**
|
|
381
551
|
* Exports records from the collection as CSV or JSON.
|
|
382
552
|
*/
|
|
@@ -1164,6 +1334,7 @@ var AppService = class extends BaseService {
|
|
|
1164
1334
|
var NodeStackClient = class _NodeStackClient {
|
|
1165
1335
|
baseUrl;
|
|
1166
1336
|
authStore;
|
|
1337
|
+
apiKey;
|
|
1167
1338
|
files;
|
|
1168
1339
|
realtime;
|
|
1169
1340
|
admins;
|
|
@@ -1181,6 +1352,7 @@ var NodeStackClient = class _NodeStackClient {
|
|
|
1181
1352
|
constructor(baseUrl = "/", options = {}) {
|
|
1182
1353
|
this.baseUrl = (baseUrl || "/").replace(/\/+$/, "");
|
|
1183
1354
|
this.authStore = options.authStore || new LocalAuthStore();
|
|
1355
|
+
this.apiKey = options.apiKey;
|
|
1184
1356
|
this.customFetch = options.fetch;
|
|
1185
1357
|
this.defaultHeaders = options.headers || {};
|
|
1186
1358
|
this.defaultTimeout = options.timeout || 12e4;
|
|
@@ -1244,7 +1416,9 @@ var NodeStackClient = class _NodeStackClient {
|
|
|
1244
1416
|
...this.defaultHeaders,
|
|
1245
1417
|
...options.headers || {}
|
|
1246
1418
|
};
|
|
1247
|
-
if (this.
|
|
1419
|
+
if (this.apiKey && !headers["Authorization"] && !headers["authorization"] && !headers["x-api-key"] && !headers["X-API-Key"]) {
|
|
1420
|
+
headers["Authorization"] = `Bearer ${this.apiKey}`;
|
|
1421
|
+
} else if (this.authStore.token && !headers["Authorization"] && !headers["authorization"]) {
|
|
1248
1422
|
headers["Authorization"] = `Bearer ${this.authStore.token}`;
|
|
1249
1423
|
}
|
|
1250
1424
|
if (this.appSlug && !headers["x-nodestack-app"] && !headers["X-NodeStack-App"]) {
|
|
@@ -1387,4 +1561,90 @@ var NodeStackClient = class _NodeStackClient {
|
|
|
1387
1561
|
var MemoryAuthStore = class extends BaseAuthStore {
|
|
1388
1562
|
// Inherits default in-memory behavior of BaseAuthStore
|
|
1389
1563
|
};
|
|
1564
|
+
|
|
1565
|
+
// src/auth/PkceUtil.ts
|
|
1566
|
+
var PkceUtil = class _PkceUtil {
|
|
1567
|
+
static generateVerifier(length = 64) {
|
|
1568
|
+
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
|
|
1569
|
+
const clampedLength = Math.max(43, Math.min(128, length));
|
|
1570
|
+
const randomBytes = new Uint8Array(clampedLength);
|
|
1571
|
+
const g = typeof globalThis !== "undefined" ? globalThis : {};
|
|
1572
|
+
if (typeof crypto !== "undefined" && crypto.getRandomValues) {
|
|
1573
|
+
crypto.getRandomValues(randomBytes);
|
|
1574
|
+
} else if (g.crypto && g.crypto.getRandomValues) {
|
|
1575
|
+
g.crypto.getRandomValues(randomBytes);
|
|
1576
|
+
} else {
|
|
1577
|
+
let filled = false;
|
|
1578
|
+
try {
|
|
1579
|
+
const req = g.require;
|
|
1580
|
+
if (typeof req === "function") {
|
|
1581
|
+
req("crypto").randomFillSync(randomBytes);
|
|
1582
|
+
filled = true;
|
|
1583
|
+
}
|
|
1584
|
+
} catch {
|
|
1585
|
+
}
|
|
1586
|
+
if (!filled) {
|
|
1587
|
+
throw new Error(
|
|
1588
|
+
"A cryptographically secure random number generator (crypto.getRandomValues or node:crypto) is required to generate a PKCE code_verifier."
|
|
1589
|
+
);
|
|
1590
|
+
}
|
|
1591
|
+
}
|
|
1592
|
+
let result = "";
|
|
1593
|
+
for (let i = 0; i < clampedLength; i++) {
|
|
1594
|
+
result += charset[randomBytes[i] % charset.length];
|
|
1595
|
+
}
|
|
1596
|
+
return result;
|
|
1597
|
+
}
|
|
1598
|
+
static async generateChallenge(verifier) {
|
|
1599
|
+
const encoder = new TextEncoder();
|
|
1600
|
+
const data = encoder.encode(verifier);
|
|
1601
|
+
const g = typeof globalThis !== "undefined" ? globalThis : {};
|
|
1602
|
+
if (typeof crypto !== "undefined" && crypto.subtle) {
|
|
1603
|
+
const hash = await crypto.subtle.digest("SHA-256", data);
|
|
1604
|
+
return _PkceUtil.base64UrlEncode(new Uint8Array(hash));
|
|
1605
|
+
} else if (g.crypto && g.crypto.subtle) {
|
|
1606
|
+
const hash = await g.crypto.subtle.digest("SHA-256", data);
|
|
1607
|
+
return _PkceUtil.base64UrlEncode(new Uint8Array(hash));
|
|
1608
|
+
}
|
|
1609
|
+
try {
|
|
1610
|
+
const req = g.require;
|
|
1611
|
+
if (typeof req === "function") {
|
|
1612
|
+
const hash = req("crypto").createHash("sha256").update(data).digest();
|
|
1613
|
+
return _PkceUtil.base64UrlEncode(new Uint8Array(hash));
|
|
1614
|
+
}
|
|
1615
|
+
} catch {
|
|
1616
|
+
}
|
|
1617
|
+
throw new Error("Web Crypto API (crypto.subtle) is not available to compute SHA-256 code challenge.");
|
|
1618
|
+
}
|
|
1619
|
+
static base64UrlEncode(bytes) {
|
|
1620
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
1621
|
+
let base64 = "";
|
|
1622
|
+
const byteLength = bytes.byteLength;
|
|
1623
|
+
const byteRemainder = byteLength % 3;
|
|
1624
|
+
const mainLength = byteLength - byteRemainder;
|
|
1625
|
+
let a, b, c, d;
|
|
1626
|
+
let chunk;
|
|
1627
|
+
for (let i = 0; i < mainLength; i += 3) {
|
|
1628
|
+
chunk = bytes[i] << 16 | bytes[i + 1] << 8 | bytes[i + 2];
|
|
1629
|
+
a = (chunk & 16515072) >> 18;
|
|
1630
|
+
b = (chunk & 258048) >> 12;
|
|
1631
|
+
c = (chunk & 4032) >> 6;
|
|
1632
|
+
d = chunk & 63;
|
|
1633
|
+
base64 += chars[a] + chars[b] + chars[c] + chars[d];
|
|
1634
|
+
}
|
|
1635
|
+
if (byteRemainder === 1) {
|
|
1636
|
+
chunk = bytes[mainLength];
|
|
1637
|
+
a = (chunk & 252) >> 2;
|
|
1638
|
+
b = (chunk & 3) << 4;
|
|
1639
|
+
base64 += chars[a] + chars[b] + "==";
|
|
1640
|
+
} else if (byteRemainder === 2) {
|
|
1641
|
+
chunk = bytes[mainLength] << 8 | bytes[mainLength + 1];
|
|
1642
|
+
a = (chunk & 64512) >> 10;
|
|
1643
|
+
b = (chunk & 1008) >> 4;
|
|
1644
|
+
c = (chunk & 15) << 2;
|
|
1645
|
+
base64 += chars[a] + chars[b] + chars[c] + "=";
|
|
1646
|
+
}
|
|
1647
|
+
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
1648
|
+
}
|
|
1649
|
+
};
|
|
1390
1650
|
//# sourceMappingURL=index.cjs.map
|