@learncard/network-brain-client 2.5.46 → 2.5.47

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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAoB,UAAU,EAAiB,MAAM,cAAc,CAAC;AAC3E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAIlE,MAAM,MAAM,SAAS,GAAG,UAAU,CAAC,SAAS,CAAC,CAAC;AAE9C,MAAM,MAAM,sBAAsB,GAAG,MAAM,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;AAE5F,eAAO,MAAM,SAAS,QACb,MAAM,mBACM,CAAC,SAAS,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,2BAC/B,sBAAsB,iBAChC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KACtC,OAAO,CAAC,SAAS,CAyDnB,CAAC;AAGF,eAAO,MAAM,iBAAiB,QACrB,MAAM,YACD,MAAM,2BACS,sBAAsB,iBAChC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KACtC,OAAO,CAAC,SAAS,CA2BnB,CAAC;AAEF,eAAe,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAoB,UAAU,EAAiB,MAAM,cAAc,CAAC;AAC3E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAIlE,MAAM,MAAM,SAAS,GAAG,UAAU,CAAC,SAAS,CAAC,CAAC;AAE9C,MAAM,MAAM,sBAAsB,GAAG,MAAM,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;AAE5F,eAAO,MAAM,SAAS,QACb,MAAM,mBACM,CAAC,SAAS,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,2BAC/B,sBAAsB,iBAChC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KACtC,OAAO,CAAC,SAAS,CAoFnB,CAAC;AAGF,eAAO,MAAM,iBAAiB,QACrB,MAAM,YACD,MAAM,2BACS,sBAAsB,iBAChC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KACtC,OAAO,CAAC,SAAS,CA2BnB,CAAC;AAEF,eAAe,SAAS,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@learncard/network-brain-client",
3
- "version": "2.5.46",
3
+ "version": "2.5.47",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -24,7 +24,9 @@
24
24
  "src"
25
25
  ],
26
26
  "scripts": {
27
- "build": "node ./scripts/build.mjs && shx cp ./scripts/mixedEntypoint.js ./dist/index.cjs && tsc --p tsconfig.json && shx cp ./dist/index.d.ts ./dist/index.d.cts"
27
+ "build": "node ./scripts/build.mjs && shx cp ./scripts/mixedEntypoint.js ./dist/index.cjs && tsc --p tsconfig.json && shx cp ./dist/index.d.ts ./dist/index.d.cts",
28
+ "test": "vitest run",
29
+ "test:watch": "vitest"
28
30
  },
29
31
  "keywords": [],
30
32
  "author": "Learning Economy Foundation (www.learningeconomy.io)",
@@ -33,10 +35,11 @@
33
35
  "@learncard/helpers": "1.3.9",
34
36
  "@learncard/types": "5.17.6",
35
37
  "dts-bundle-generator": "^6.10.0",
36
- "esbuild": "^0.27.1"
38
+ "esbuild": "^0.27.1",
39
+ "vitest": "^1.4.0"
37
40
  },
38
41
  "dependencies": {
39
- "@learncard/network-brain-service": "3.16.9",
42
+ "@learncard/network-brain-service": "3.16.10",
40
43
  "@trpc/client": "11.3.0"
41
44
  },
42
45
  "repository": {
package/src/index.ts CHANGED
@@ -37,7 +37,34 @@ export const getClient = async (
37
37
  return challengeRequester.utilities.getChallenges.query({ amount });
38
38
  };
39
39
 
40
- getChallenges().then(result => (challenges = result));
40
+ let refillPromise: Promise<void> | undefined;
41
+
42
+ const refillChallenges = (): Promise<void> => {
43
+ refillPromise ??= getChallenges()
44
+ .then(result => {
45
+ if (result.length === 0) throw new Error('Challenge refill returned no challenges');
46
+
47
+ challenges.push(...result);
48
+ })
49
+ .finally(() => {
50
+ refillPromise = undefined;
51
+ });
52
+
53
+ return refillPromise;
54
+ };
55
+
56
+ const takeChallenge = async (): Promise<string> => {
57
+ while (true) {
58
+ const challenge = challenges.pop();
59
+ if (challenge !== undefined) return challenge;
60
+
61
+ await refillChallenges();
62
+ }
63
+ };
64
+
65
+ // Pre-warm the pool while the caller continues client/plugin setup. Requests
66
+ // arriving before this settles await the same single-flight refill.
67
+ void refillChallenges().catch(() => undefined);
41
68
 
42
69
  const trpc = createTRPCClient<AppRouter>({
43
70
  links: [
@@ -49,14 +76,14 @@ export const getClient = async (
49
76
  maxURLLength: 3072,
50
77
  url,
51
78
  headers: async () => {
52
- if (challenges.length === 0) challenges.push(...(await getChallenges()));
79
+ const challenge = await takeChallenge();
53
80
 
54
81
  const guardianApproval = guardianApprovalGetter
55
82
  ? await guardianApprovalGetter()
56
83
  : undefined;
57
84
 
58
85
  return {
59
- Authorization: `Bearer ${await didAuthFunction(challenges.pop())}`,
86
+ Authorization: `Bearer ${await didAuthFunction(challenge)}`,
60
87
  ...(guardianApproval ? { 'x-guardian-approval': guardianApproval } : {}),
61
88
  ...extraHeaders,
62
89
  };