@smartcrab/server 0.3.2 → 0.4.0-next-20261017141739

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/base64.js CHANGED
@@ -4,28 +4,15 @@
4
4
  * alphabet handling in @smartcrab/identifiers, which is deliberately not a
5
5
  * dependency of this package.
6
6
  */
7
- const BASE64URL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
8
7
  import { base64url } from "jose";
9
8
  /** base64url, no padding (RFC 4648 §5). Suitable for secrets, tokens and cookie values. */
10
9
  export const base64UrlEncode = (bytes) => base64url.encode(bytes);
11
10
  /** Throws on invalid characters — call only inside a `Result.fn` boundary. */
12
11
  export const base64UrlDecode = (encoded) => {
13
- const out = [];
14
- let buffer = 0;
15
- let bits = 0;
16
- for (const char of encoded) {
17
- const index = BASE64URL_ALPHABET.indexOf(char);
18
- if (index < 0) {
19
- throw new Error("invalid base64url input");
20
- }
21
- buffer = (buffer << 6) | index;
22
- bits += 6;
23
- if (bits >= 8) {
24
- bits -= 8;
25
- out.push((buffer >>> bits) & 0xff);
26
- }
27
- }
28
- return new Uint8Array(out);
12
+ if (!BASE64URL_BODY_PATTERN.test(encoded))
13
+ throw new Error("invalid base64url input");
14
+ const complete = encoded.length % 4 === 1 ? encoded.slice(0, -1) : encoded;
15
+ return new Uint8Array(base64url.decode(complete));
29
16
  };
30
17
  /** Standard base64 (RFC 4648 §4, with padding) — used for HTTP Basic client auth. */
31
18
  export const base64StandardEncode = (bytes) => {
package/dist/sealing.js CHANGED
@@ -34,8 +34,7 @@ const deriveAesKey = Result.fn({
34
34
  });
35
35
  const encrypt = Result.fn({
36
36
  try: async (input) => {
37
- const iv = new Uint8Array(GCM_IV_BYTES);
38
- crypto.getRandomValues(iv);
37
+ const iv = crypto.getRandomValues(new Uint8Array(GCM_IV_BYTES));
39
38
  const ciphertext = await crypto.subtle.encrypt({ name: "AES-GCM", iv }, input.key, textEncoder.encode(JSON.stringify(input.payload)));
40
39
  return `${SEAL_VERSION}.${base64UrlEncode(iv)}.${base64UrlEncode(new Uint8Array(ciphertext))}`;
41
40
  },
package/dist/security.js CHANGED
@@ -13,8 +13,7 @@ import { Result } from "./result.js";
13
13
  const textEncoder = new TextEncoder();
14
14
  /** Cryptographically random opaque string (base64url, default 256 bits). */
15
15
  export const generateOpaqueToken = (bytes = 32) => {
16
- const buffer = new Uint8Array(bytes);
17
- crypto.getRandomValues(buffer);
16
+ const buffer = crypto.getRandomValues(new Uint8Array(bytes));
18
17
  return base64UrlEncode(buffer);
19
18
  };
20
19
  const sha256Base64Url = Result.fn({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smartcrab/server",
3
- "version": "0.3.2",
3
+ "version": "0.4.0-next-20261017141739",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -29,7 +29,7 @@
29
29
  "dependencies": {
30
30
  "jose": "6.2.12",
31
31
  "cookie": "2.0.1",
32
- "@smartcrab/contracts-public": "0.3.2"
32
+ "@smartcrab/contracts-public": "0.4.0-next-20261017141739"
33
33
  },
34
34
  "devDependencies": {
35
35
  "typescript": "5.9.3",
package/src/base64.ts CHANGED
@@ -5,8 +5,6 @@
5
5
  * dependency of this package.
6
6
  */
7
7
 
8
- const BASE64URL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
9
-
10
8
  import { base64url } from "jose";
11
9
 
12
10
  /** base64url, no padding (RFC 4648 §5). Suitable for secrets, tokens and cookie values. */
@@ -14,22 +12,9 @@ export const base64UrlEncode = (bytes: Uint8Array): string => base64url.encode(b
14
12
 
15
13
  /** Throws on invalid characters — call only inside a `Result.fn` boundary. */
16
14
  export const base64UrlDecode = (encoded: string): Uint8Array<ArrayBuffer> => {
17
- const out: number[] = [];
18
- let buffer = 0;
19
- let bits = 0;
20
- for (const char of encoded) {
21
- const index = BASE64URL_ALPHABET.indexOf(char);
22
- if (index < 0) {
23
- throw new Error("invalid base64url input");
24
- }
25
- buffer = (buffer << 6) | index;
26
- bits += 6;
27
- if (bits >= 8) {
28
- bits -= 8;
29
- out.push((buffer >>> bits) & 0xff);
30
- }
31
- }
32
- return new Uint8Array(out);
15
+ if (!BASE64URL_BODY_PATTERN.test(encoded)) throw new Error("invalid base64url input");
16
+ const complete = encoded.length % 4 === 1 ? encoded.slice(0, -1) : encoded;
17
+ return new Uint8Array(base64url.decode(complete));
33
18
  };
34
19
 
35
20
  /** Standard base64 (RFC 4648 §4, with padding) — used for HTTP Basic client auth. */
package/src/sealing.ts CHANGED
@@ -55,8 +55,7 @@ const deriveAesKey = Result.fn({
55
55
 
56
56
  const encrypt = Result.fn({
57
57
  try: async (input: { readonly key: CryptoKey; readonly payload: unknown }): Promise<string> => {
58
- const iv = new Uint8Array(GCM_IV_BYTES);
59
- crypto.getRandomValues(iv);
58
+ const iv = crypto.getRandomValues(new Uint8Array(GCM_IV_BYTES));
60
59
  const ciphertext = await crypto.subtle.encrypt(
61
60
  { name: "AES-GCM", iv },
62
61
  input.key,
package/src/security.ts CHANGED
@@ -17,8 +17,7 @@ const textEncoder = new TextEncoder();
17
17
 
18
18
  /** Cryptographically random opaque string (base64url, default 256 bits). */
19
19
  export const generateOpaqueToken = (bytes = 32): string => {
20
- const buffer = new Uint8Array(bytes);
21
- crypto.getRandomValues(buffer);
20
+ const buffer = crypto.getRandomValues(new Uint8Array(bytes));
22
21
  return base64UrlEncode(buffer);
23
22
  };
24
23