@prmichaelsen/firebase-admin-sdk-v8 2.0.17 → 2.0.19
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/index.d.mts +26 -26
- package/dist/index.d.ts +26 -26
- package/dist/index.js +73 -73
- package/dist/index.mjs +73 -73
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -287,18 +287,18 @@ declare function getUserFromToken(idToken: string): Promise<UserInfo>;
|
|
|
287
287
|
declare function getAuth(): any;
|
|
288
288
|
|
|
289
289
|
/**
|
|
290
|
-
* Firebase Admin SDK v8 - Firestore
|
|
291
|
-
*
|
|
290
|
+
* Firebase Admin SDK v8 - Firestore CRUD Operations
|
|
291
|
+
* All Firestore document operations using REST API
|
|
292
292
|
*/
|
|
293
293
|
|
|
294
294
|
/**
|
|
295
295
|
* Set a document in Firestore (create or overwrite)
|
|
296
296
|
*
|
|
297
|
-
* @param
|
|
298
|
-
* @param
|
|
299
|
-
* @param
|
|
300
|
-
* @param
|
|
301
|
-
* @returns
|
|
297
|
+
* @param collectionPath - Collection path
|
|
298
|
+
* @param documentId - Document ID
|
|
299
|
+
* @param data - Document data
|
|
300
|
+
* @param options - Set options (merge, mergeFields)
|
|
301
|
+
* @returns Promise that resolves when document is set
|
|
302
302
|
* @throws {Error} If the operation fails
|
|
303
303
|
*
|
|
304
304
|
* @example
|
|
@@ -317,10 +317,10 @@ declare function setDocument(collectionPath: string, documentId: string, data: D
|
|
|
317
317
|
/**
|
|
318
318
|
* Add a document to Firestore collection
|
|
319
319
|
*
|
|
320
|
-
* @param
|
|
321
|
-
* @param
|
|
322
|
-
* @param
|
|
323
|
-
* @returns
|
|
320
|
+
* @param collectionPath - Collection path (e.g., 'users' or 'users/uid/posts')
|
|
321
|
+
* @param data - Document data
|
|
322
|
+
* @param documentId - Optional document ID (auto-generated if not provided)
|
|
323
|
+
* @returns Document reference with id and path
|
|
324
324
|
* @throws {Error} If the operation fails
|
|
325
325
|
*
|
|
326
326
|
* @example
|
|
@@ -337,9 +337,9 @@ declare function addDocument(collectionPath: string, data: DataObject, documentI
|
|
|
337
337
|
/**
|
|
338
338
|
* Get a document from Firestore
|
|
339
339
|
*
|
|
340
|
-
* @param
|
|
341
|
-
* @param
|
|
342
|
-
* @returns
|
|
340
|
+
* @param collectionPath - Collection path
|
|
341
|
+
* @param documentId - Document ID
|
|
342
|
+
* @returns Document data or null if not found
|
|
343
343
|
* @throws {Error} If the operation fails
|
|
344
344
|
*
|
|
345
345
|
* @example
|
|
@@ -354,10 +354,10 @@ declare function getDocument(collectionPath: string, documentId: string): Promis
|
|
|
354
354
|
/**
|
|
355
355
|
* Update a document in Firestore
|
|
356
356
|
*
|
|
357
|
-
* @param
|
|
358
|
-
* @param
|
|
359
|
-
* @param
|
|
360
|
-
* @returns
|
|
357
|
+
* @param collectionPath - Collection path
|
|
358
|
+
* @param documentId - Document ID
|
|
359
|
+
* @param data - Data to update
|
|
360
|
+
* @returns Promise that resolves when document is updated
|
|
361
361
|
* @throws {Error} If the operation fails
|
|
362
362
|
*
|
|
363
363
|
* @example
|
|
@@ -372,9 +372,9 @@ declare function updateDocument(collectionPath: string, documentId: string, data
|
|
|
372
372
|
/**
|
|
373
373
|
* Delete a document from Firestore
|
|
374
374
|
*
|
|
375
|
-
* @param
|
|
376
|
-
* @param
|
|
377
|
-
* @returns
|
|
375
|
+
* @param collectionPath - Collection path
|
|
376
|
+
* @param documentId - Document ID
|
|
377
|
+
* @returns Promise that resolves when document is deleted
|
|
378
378
|
* @throws {Error} If the operation fails
|
|
379
379
|
*
|
|
380
380
|
* @example
|
|
@@ -386,9 +386,9 @@ declare function deleteDocument(collectionPath: string, documentId: string): Pro
|
|
|
386
386
|
/**
|
|
387
387
|
* Query documents in a collection with advanced filtering
|
|
388
388
|
*
|
|
389
|
-
* @param
|
|
390
|
-
* @param
|
|
391
|
-
* @returns
|
|
389
|
+
* @param collectionPath - Collection path
|
|
390
|
+
* @param options - Query options (where, orderBy, limit, etc.)
|
|
391
|
+
* @returns Array of documents with id and data
|
|
392
392
|
* @throws {Error} If the operation fails
|
|
393
393
|
*
|
|
394
394
|
* @example
|
|
@@ -410,8 +410,8 @@ declare function queryDocuments(collectionPath: string, options?: QueryOptions):
|
|
|
410
410
|
/**
|
|
411
411
|
* Perform batch write operations (set, update, delete)
|
|
412
412
|
*
|
|
413
|
-
* @param
|
|
414
|
-
* @returns
|
|
413
|
+
* @param operations - Array of batch operations
|
|
414
|
+
* @returns Batch write result
|
|
415
415
|
* @throws {Error} If the operation fails
|
|
416
416
|
*
|
|
417
417
|
* @example
|
package/dist/index.d.ts
CHANGED
|
@@ -287,18 +287,18 @@ declare function getUserFromToken(idToken: string): Promise<UserInfo>;
|
|
|
287
287
|
declare function getAuth(): any;
|
|
288
288
|
|
|
289
289
|
/**
|
|
290
|
-
* Firebase Admin SDK v8 - Firestore
|
|
291
|
-
*
|
|
290
|
+
* Firebase Admin SDK v8 - Firestore CRUD Operations
|
|
291
|
+
* All Firestore document operations using REST API
|
|
292
292
|
*/
|
|
293
293
|
|
|
294
294
|
/**
|
|
295
295
|
* Set a document in Firestore (create or overwrite)
|
|
296
296
|
*
|
|
297
|
-
* @param
|
|
298
|
-
* @param
|
|
299
|
-
* @param
|
|
300
|
-
* @param
|
|
301
|
-
* @returns
|
|
297
|
+
* @param collectionPath - Collection path
|
|
298
|
+
* @param documentId - Document ID
|
|
299
|
+
* @param data - Document data
|
|
300
|
+
* @param options - Set options (merge, mergeFields)
|
|
301
|
+
* @returns Promise that resolves when document is set
|
|
302
302
|
* @throws {Error} If the operation fails
|
|
303
303
|
*
|
|
304
304
|
* @example
|
|
@@ -317,10 +317,10 @@ declare function setDocument(collectionPath: string, documentId: string, data: D
|
|
|
317
317
|
/**
|
|
318
318
|
* Add a document to Firestore collection
|
|
319
319
|
*
|
|
320
|
-
* @param
|
|
321
|
-
* @param
|
|
322
|
-
* @param
|
|
323
|
-
* @returns
|
|
320
|
+
* @param collectionPath - Collection path (e.g., 'users' or 'users/uid/posts')
|
|
321
|
+
* @param data - Document data
|
|
322
|
+
* @param documentId - Optional document ID (auto-generated if not provided)
|
|
323
|
+
* @returns Document reference with id and path
|
|
324
324
|
* @throws {Error} If the operation fails
|
|
325
325
|
*
|
|
326
326
|
* @example
|
|
@@ -337,9 +337,9 @@ declare function addDocument(collectionPath: string, data: DataObject, documentI
|
|
|
337
337
|
/**
|
|
338
338
|
* Get a document from Firestore
|
|
339
339
|
*
|
|
340
|
-
* @param
|
|
341
|
-
* @param
|
|
342
|
-
* @returns
|
|
340
|
+
* @param collectionPath - Collection path
|
|
341
|
+
* @param documentId - Document ID
|
|
342
|
+
* @returns Document data or null if not found
|
|
343
343
|
* @throws {Error} If the operation fails
|
|
344
344
|
*
|
|
345
345
|
* @example
|
|
@@ -354,10 +354,10 @@ declare function getDocument(collectionPath: string, documentId: string): Promis
|
|
|
354
354
|
/**
|
|
355
355
|
* Update a document in Firestore
|
|
356
356
|
*
|
|
357
|
-
* @param
|
|
358
|
-
* @param
|
|
359
|
-
* @param
|
|
360
|
-
* @returns
|
|
357
|
+
* @param collectionPath - Collection path
|
|
358
|
+
* @param documentId - Document ID
|
|
359
|
+
* @param data - Data to update
|
|
360
|
+
* @returns Promise that resolves when document is updated
|
|
361
361
|
* @throws {Error} If the operation fails
|
|
362
362
|
*
|
|
363
363
|
* @example
|
|
@@ -372,9 +372,9 @@ declare function updateDocument(collectionPath: string, documentId: string, data
|
|
|
372
372
|
/**
|
|
373
373
|
* Delete a document from Firestore
|
|
374
374
|
*
|
|
375
|
-
* @param
|
|
376
|
-
* @param
|
|
377
|
-
* @returns
|
|
375
|
+
* @param collectionPath - Collection path
|
|
376
|
+
* @param documentId - Document ID
|
|
377
|
+
* @returns Promise that resolves when document is deleted
|
|
378
378
|
* @throws {Error} If the operation fails
|
|
379
379
|
*
|
|
380
380
|
* @example
|
|
@@ -386,9 +386,9 @@ declare function deleteDocument(collectionPath: string, documentId: string): Pro
|
|
|
386
386
|
/**
|
|
387
387
|
* Query documents in a collection with advanced filtering
|
|
388
388
|
*
|
|
389
|
-
* @param
|
|
390
|
-
* @param
|
|
391
|
-
* @returns
|
|
389
|
+
* @param collectionPath - Collection path
|
|
390
|
+
* @param options - Query options (where, orderBy, limit, etc.)
|
|
391
|
+
* @returns Array of documents with id and data
|
|
392
392
|
* @throws {Error} If the operation fails
|
|
393
393
|
*
|
|
394
394
|
* @example
|
|
@@ -410,8 +410,8 @@ declare function queryDocuments(collectionPath: string, options?: QueryOptions):
|
|
|
410
410
|
/**
|
|
411
411
|
* Perform batch write operations (set, update, delete)
|
|
412
412
|
*
|
|
413
|
-
* @param
|
|
414
|
-
* @returns
|
|
413
|
+
* @param operations - Array of batch operations
|
|
414
|
+
* @returns Batch write result
|
|
415
415
|
* @throws {Error} If the operation fails
|
|
416
416
|
*
|
|
417
417
|
* @example
|
package/dist/index.js
CHANGED
|
@@ -331,78 +331,6 @@ function getAuth() {
|
|
|
331
331
|
};
|
|
332
332
|
}
|
|
333
333
|
|
|
334
|
-
// src/token-generation.ts
|
|
335
|
-
function base64UrlEncode(str) {
|
|
336
|
-
return btoa(str).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
337
|
-
}
|
|
338
|
-
function base64UrlEncodeBuffer(buffer) {
|
|
339
|
-
return btoa(String.fromCharCode(...buffer)).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
340
|
-
}
|
|
341
|
-
async function createJWT(serviceAccount) {
|
|
342
|
-
const now = Math.floor(Date.now() / 1e3);
|
|
343
|
-
const expiry = now + 3600;
|
|
344
|
-
const header = {
|
|
345
|
-
alg: "RS256",
|
|
346
|
-
typ: "JWT"
|
|
347
|
-
};
|
|
348
|
-
const payload = {
|
|
349
|
-
iss: serviceAccount.client_email,
|
|
350
|
-
sub: serviceAccount.client_email,
|
|
351
|
-
aud: serviceAccount.token_uri,
|
|
352
|
-
iat: now,
|
|
353
|
-
exp: expiry,
|
|
354
|
-
scope: "https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/datastore https://www.googleapis.com/auth/firebase"
|
|
355
|
-
};
|
|
356
|
-
const encodedHeader = base64UrlEncode(JSON.stringify(header));
|
|
357
|
-
const encodedPayload = base64UrlEncode(JSON.stringify(payload));
|
|
358
|
-
const unsignedToken = `${encodedHeader}.${encodedPayload}`;
|
|
359
|
-
const pemContents = serviceAccount.private_key.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "").replace(/\s/g, "");
|
|
360
|
-
const binaryDer = Uint8Array.from(atob(pemContents), (c) => c.charCodeAt(0));
|
|
361
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
362
|
-
"pkcs8",
|
|
363
|
-
binaryDer,
|
|
364
|
-
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
|
365
|
-
false,
|
|
366
|
-
["sign"]
|
|
367
|
-
);
|
|
368
|
-
const signature = await crypto.subtle.sign(
|
|
369
|
-
"RSASSA-PKCS1-v1_5",
|
|
370
|
-
cryptoKey,
|
|
371
|
-
new TextEncoder().encode(unsignedToken)
|
|
372
|
-
);
|
|
373
|
-
const encodedSignature = base64UrlEncodeBuffer(new Uint8Array(signature));
|
|
374
|
-
return `${unsignedToken}.${encodedSignature}`;
|
|
375
|
-
}
|
|
376
|
-
var cachedAccessToken = null;
|
|
377
|
-
var tokenExpiry = 0;
|
|
378
|
-
async function getAdminAccessToken() {
|
|
379
|
-
if (cachedAccessToken && Date.now() < tokenExpiry) {
|
|
380
|
-
return cachedAccessToken;
|
|
381
|
-
}
|
|
382
|
-
const serviceAccount = getServiceAccount();
|
|
383
|
-
const jwt = await createJWT(serviceAccount);
|
|
384
|
-
const response = await fetch(serviceAccount.token_uri, {
|
|
385
|
-
method: "POST",
|
|
386
|
-
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
387
|
-
body: new URLSearchParams({
|
|
388
|
-
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
|
389
|
-
assertion: jwt
|
|
390
|
-
})
|
|
391
|
-
});
|
|
392
|
-
if (!response.ok) {
|
|
393
|
-
const errorText = await response.text();
|
|
394
|
-
throw new Error(`Failed to get access token: ${errorText}`);
|
|
395
|
-
}
|
|
396
|
-
const data = await response.json();
|
|
397
|
-
cachedAccessToken = data.access_token;
|
|
398
|
-
tokenExpiry = Date.now() + data.expires_in * 1e3 - 6e4;
|
|
399
|
-
return cachedAccessToken;
|
|
400
|
-
}
|
|
401
|
-
function clearTokenCache() {
|
|
402
|
-
cachedAccessToken = null;
|
|
403
|
-
tokenExpiry = 0;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
334
|
// src/field-value.ts
|
|
407
335
|
function serverTimestamp() {
|
|
408
336
|
return {
|
|
@@ -679,7 +607,79 @@ function removeFieldTransforms(data) {
|
|
|
679
607
|
return result;
|
|
680
608
|
}
|
|
681
609
|
|
|
682
|
-
// src/
|
|
610
|
+
// src/token-generation.ts
|
|
611
|
+
function base64UrlEncode(str) {
|
|
612
|
+
return btoa(str).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
613
|
+
}
|
|
614
|
+
function base64UrlEncodeBuffer(buffer) {
|
|
615
|
+
return btoa(String.fromCharCode(...buffer)).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
616
|
+
}
|
|
617
|
+
async function createJWT(serviceAccount) {
|
|
618
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
619
|
+
const expiry = now + 3600;
|
|
620
|
+
const header = {
|
|
621
|
+
alg: "RS256",
|
|
622
|
+
typ: "JWT"
|
|
623
|
+
};
|
|
624
|
+
const payload = {
|
|
625
|
+
iss: serviceAccount.client_email,
|
|
626
|
+
sub: serviceAccount.client_email,
|
|
627
|
+
aud: serviceAccount.token_uri,
|
|
628
|
+
iat: now,
|
|
629
|
+
exp: expiry,
|
|
630
|
+
scope: "https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/datastore https://www.googleapis.com/auth/firebase"
|
|
631
|
+
};
|
|
632
|
+
const encodedHeader = base64UrlEncode(JSON.stringify(header));
|
|
633
|
+
const encodedPayload = base64UrlEncode(JSON.stringify(payload));
|
|
634
|
+
const unsignedToken = `${encodedHeader}.${encodedPayload}`;
|
|
635
|
+
const pemContents = serviceAccount.private_key.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "").replace(/\s/g, "");
|
|
636
|
+
const binaryDer = Uint8Array.from(atob(pemContents), (c) => c.charCodeAt(0));
|
|
637
|
+
const cryptoKey = await crypto.subtle.importKey(
|
|
638
|
+
"pkcs8",
|
|
639
|
+
binaryDer,
|
|
640
|
+
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
|
641
|
+
false,
|
|
642
|
+
["sign"]
|
|
643
|
+
);
|
|
644
|
+
const signature = await crypto.subtle.sign(
|
|
645
|
+
"RSASSA-PKCS1-v1_5",
|
|
646
|
+
cryptoKey,
|
|
647
|
+
new TextEncoder().encode(unsignedToken)
|
|
648
|
+
);
|
|
649
|
+
const encodedSignature = base64UrlEncodeBuffer(new Uint8Array(signature));
|
|
650
|
+
return `${unsignedToken}.${encodedSignature}`;
|
|
651
|
+
}
|
|
652
|
+
var cachedAccessToken = null;
|
|
653
|
+
var tokenExpiry = 0;
|
|
654
|
+
async function getAdminAccessToken() {
|
|
655
|
+
if (cachedAccessToken && Date.now() < tokenExpiry) {
|
|
656
|
+
return cachedAccessToken;
|
|
657
|
+
}
|
|
658
|
+
const serviceAccount = getServiceAccount();
|
|
659
|
+
const jwt = await createJWT(serviceAccount);
|
|
660
|
+
const response = await fetch(serviceAccount.token_uri, {
|
|
661
|
+
method: "POST",
|
|
662
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
663
|
+
body: new URLSearchParams({
|
|
664
|
+
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
|
665
|
+
assertion: jwt
|
|
666
|
+
})
|
|
667
|
+
});
|
|
668
|
+
if (!response.ok) {
|
|
669
|
+
const errorText = await response.text();
|
|
670
|
+
throw new Error(`Failed to get access token: ${errorText}`);
|
|
671
|
+
}
|
|
672
|
+
const data = await response.json();
|
|
673
|
+
cachedAccessToken = data.access_token;
|
|
674
|
+
tokenExpiry = Date.now() + data.expires_in * 1e3 - 6e4;
|
|
675
|
+
return cachedAccessToken;
|
|
676
|
+
}
|
|
677
|
+
function clearTokenCache() {
|
|
678
|
+
cachedAccessToken = null;
|
|
679
|
+
tokenExpiry = 0;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
// src/firestore/operations.ts
|
|
683
683
|
var FIRESTORE_API = "https://firestore.googleapis.com/v1";
|
|
684
684
|
async function commitWrites(writes) {
|
|
685
685
|
const accessToken = await getAdminAccessToken();
|
package/dist/index.mjs
CHANGED
|
@@ -288,78 +288,6 @@ function getAuth() {
|
|
|
288
288
|
};
|
|
289
289
|
}
|
|
290
290
|
|
|
291
|
-
// src/token-generation.ts
|
|
292
|
-
function base64UrlEncode(str) {
|
|
293
|
-
return btoa(str).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
294
|
-
}
|
|
295
|
-
function base64UrlEncodeBuffer(buffer) {
|
|
296
|
-
return btoa(String.fromCharCode(...buffer)).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
297
|
-
}
|
|
298
|
-
async function createJWT(serviceAccount) {
|
|
299
|
-
const now = Math.floor(Date.now() / 1e3);
|
|
300
|
-
const expiry = now + 3600;
|
|
301
|
-
const header = {
|
|
302
|
-
alg: "RS256",
|
|
303
|
-
typ: "JWT"
|
|
304
|
-
};
|
|
305
|
-
const payload = {
|
|
306
|
-
iss: serviceAccount.client_email,
|
|
307
|
-
sub: serviceAccount.client_email,
|
|
308
|
-
aud: serviceAccount.token_uri,
|
|
309
|
-
iat: now,
|
|
310
|
-
exp: expiry,
|
|
311
|
-
scope: "https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/datastore https://www.googleapis.com/auth/firebase"
|
|
312
|
-
};
|
|
313
|
-
const encodedHeader = base64UrlEncode(JSON.stringify(header));
|
|
314
|
-
const encodedPayload = base64UrlEncode(JSON.stringify(payload));
|
|
315
|
-
const unsignedToken = `${encodedHeader}.${encodedPayload}`;
|
|
316
|
-
const pemContents = serviceAccount.private_key.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "").replace(/\s/g, "");
|
|
317
|
-
const binaryDer = Uint8Array.from(atob(pemContents), (c) => c.charCodeAt(0));
|
|
318
|
-
const cryptoKey = await crypto.subtle.importKey(
|
|
319
|
-
"pkcs8",
|
|
320
|
-
binaryDer,
|
|
321
|
-
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
|
322
|
-
false,
|
|
323
|
-
["sign"]
|
|
324
|
-
);
|
|
325
|
-
const signature = await crypto.subtle.sign(
|
|
326
|
-
"RSASSA-PKCS1-v1_5",
|
|
327
|
-
cryptoKey,
|
|
328
|
-
new TextEncoder().encode(unsignedToken)
|
|
329
|
-
);
|
|
330
|
-
const encodedSignature = base64UrlEncodeBuffer(new Uint8Array(signature));
|
|
331
|
-
return `${unsignedToken}.${encodedSignature}`;
|
|
332
|
-
}
|
|
333
|
-
var cachedAccessToken = null;
|
|
334
|
-
var tokenExpiry = 0;
|
|
335
|
-
async function getAdminAccessToken() {
|
|
336
|
-
if (cachedAccessToken && Date.now() < tokenExpiry) {
|
|
337
|
-
return cachedAccessToken;
|
|
338
|
-
}
|
|
339
|
-
const serviceAccount = getServiceAccount();
|
|
340
|
-
const jwt = await createJWT(serviceAccount);
|
|
341
|
-
const response = await fetch(serviceAccount.token_uri, {
|
|
342
|
-
method: "POST",
|
|
343
|
-
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
344
|
-
body: new URLSearchParams({
|
|
345
|
-
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
|
346
|
-
assertion: jwt
|
|
347
|
-
})
|
|
348
|
-
});
|
|
349
|
-
if (!response.ok) {
|
|
350
|
-
const errorText = await response.text();
|
|
351
|
-
throw new Error(`Failed to get access token: ${errorText}`);
|
|
352
|
-
}
|
|
353
|
-
const data = await response.json();
|
|
354
|
-
cachedAccessToken = data.access_token;
|
|
355
|
-
tokenExpiry = Date.now() + data.expires_in * 1e3 - 6e4;
|
|
356
|
-
return cachedAccessToken;
|
|
357
|
-
}
|
|
358
|
-
function clearTokenCache() {
|
|
359
|
-
cachedAccessToken = null;
|
|
360
|
-
tokenExpiry = 0;
|
|
361
|
-
}
|
|
362
|
-
|
|
363
291
|
// src/field-value.ts
|
|
364
292
|
function serverTimestamp() {
|
|
365
293
|
return {
|
|
@@ -636,7 +564,79 @@ function removeFieldTransforms(data) {
|
|
|
636
564
|
return result;
|
|
637
565
|
}
|
|
638
566
|
|
|
639
|
-
// src/
|
|
567
|
+
// src/token-generation.ts
|
|
568
|
+
function base64UrlEncode(str) {
|
|
569
|
+
return btoa(str).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
570
|
+
}
|
|
571
|
+
function base64UrlEncodeBuffer(buffer) {
|
|
572
|
+
return btoa(String.fromCharCode(...buffer)).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
|
|
573
|
+
}
|
|
574
|
+
async function createJWT(serviceAccount) {
|
|
575
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
576
|
+
const expiry = now + 3600;
|
|
577
|
+
const header = {
|
|
578
|
+
alg: "RS256",
|
|
579
|
+
typ: "JWT"
|
|
580
|
+
};
|
|
581
|
+
const payload = {
|
|
582
|
+
iss: serviceAccount.client_email,
|
|
583
|
+
sub: serviceAccount.client_email,
|
|
584
|
+
aud: serviceAccount.token_uri,
|
|
585
|
+
iat: now,
|
|
586
|
+
exp: expiry,
|
|
587
|
+
scope: "https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/datastore https://www.googleapis.com/auth/firebase"
|
|
588
|
+
};
|
|
589
|
+
const encodedHeader = base64UrlEncode(JSON.stringify(header));
|
|
590
|
+
const encodedPayload = base64UrlEncode(JSON.stringify(payload));
|
|
591
|
+
const unsignedToken = `${encodedHeader}.${encodedPayload}`;
|
|
592
|
+
const pemContents = serviceAccount.private_key.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", "").replace(/\s/g, "");
|
|
593
|
+
const binaryDer = Uint8Array.from(atob(pemContents), (c) => c.charCodeAt(0));
|
|
594
|
+
const cryptoKey = await crypto.subtle.importKey(
|
|
595
|
+
"pkcs8",
|
|
596
|
+
binaryDer,
|
|
597
|
+
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
|
598
|
+
false,
|
|
599
|
+
["sign"]
|
|
600
|
+
);
|
|
601
|
+
const signature = await crypto.subtle.sign(
|
|
602
|
+
"RSASSA-PKCS1-v1_5",
|
|
603
|
+
cryptoKey,
|
|
604
|
+
new TextEncoder().encode(unsignedToken)
|
|
605
|
+
);
|
|
606
|
+
const encodedSignature = base64UrlEncodeBuffer(new Uint8Array(signature));
|
|
607
|
+
return `${unsignedToken}.${encodedSignature}`;
|
|
608
|
+
}
|
|
609
|
+
var cachedAccessToken = null;
|
|
610
|
+
var tokenExpiry = 0;
|
|
611
|
+
async function getAdminAccessToken() {
|
|
612
|
+
if (cachedAccessToken && Date.now() < tokenExpiry) {
|
|
613
|
+
return cachedAccessToken;
|
|
614
|
+
}
|
|
615
|
+
const serviceAccount = getServiceAccount();
|
|
616
|
+
const jwt = await createJWT(serviceAccount);
|
|
617
|
+
const response = await fetch(serviceAccount.token_uri, {
|
|
618
|
+
method: "POST",
|
|
619
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
620
|
+
body: new URLSearchParams({
|
|
621
|
+
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
|
622
|
+
assertion: jwt
|
|
623
|
+
})
|
|
624
|
+
});
|
|
625
|
+
if (!response.ok) {
|
|
626
|
+
const errorText = await response.text();
|
|
627
|
+
throw new Error(`Failed to get access token: ${errorText}`);
|
|
628
|
+
}
|
|
629
|
+
const data = await response.json();
|
|
630
|
+
cachedAccessToken = data.access_token;
|
|
631
|
+
tokenExpiry = Date.now() + data.expires_in * 1e3 - 6e4;
|
|
632
|
+
return cachedAccessToken;
|
|
633
|
+
}
|
|
634
|
+
function clearTokenCache() {
|
|
635
|
+
cachedAccessToken = null;
|
|
636
|
+
tokenExpiry = 0;
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
// src/firestore/operations.ts
|
|
640
640
|
var FIRESTORE_API = "https://firestore.googleapis.com/v1";
|
|
641
641
|
async function commitWrites(writes) {
|
|
642
642
|
const accessToken = await getAdminAccessToken();
|
package/package.json
CHANGED