@ricsam/quickjs-encoding 0.0.1 → 0.2.6
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 +48 -43
- package/dist/cjs/base64.cjs +61 -0
- package/dist/cjs/base64.cjs.map +10 -0
- package/dist/cjs/index.cjs +57 -0
- package/dist/cjs/index.cjs.map +10 -0
- package/dist/cjs/package.json +5 -0
- package/dist/cjs/setup.cjs +54 -0
- package/dist/cjs/setup.cjs.map +10 -0
- package/dist/cjs/types.cjs +26 -0
- package/dist/cjs/types.cjs.map +9 -0
- package/dist/mjs/base64.mjs +30 -0
- package/dist/mjs/base64.mjs.map +10 -0
- package/dist/mjs/index.mjs +9 -0
- package/dist/mjs/index.mjs.map +10 -0
- package/dist/mjs/package.json +5 -0
- package/dist/mjs/setup.mjs +23 -0
- package/dist/mjs/setup.mjs.map +10 -0
- package/dist/mjs/types.mjs +3 -0
- package/dist/mjs/types.mjs.map +9 -0
- package/dist/types/base64.d.ts +18 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/setup.d.ts +24 -0
- package/dist/types/types.d.ts +20 -0
- package/package.json +51 -6
package/README.md
CHANGED
|
@@ -1,45 +1,50 @@
|
|
|
1
1
|
# @ricsam/quickjs-encoding
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
3
|
+
Base64 encoding and decoding via `atob` and `btoa`.
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
import { setupEncoding } from "@ricsam/quickjs-encoding";
|
|
7
|
+
|
|
8
|
+
const handle = setupEncoding(context);
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**Injected Globals:**
|
|
12
|
+
- `atob(encodedData)` - Decode a Base64-encoded string
|
|
13
|
+
- `btoa(stringToEncode)` - Encode a string to Base64
|
|
14
|
+
|
|
15
|
+
**Usage in QuickJS:**
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
// Encode string to Base64
|
|
19
|
+
const encoded = btoa("Hello, World!");
|
|
20
|
+
console.log(encoded); // "SGVsbG8sIFdvcmxkIQ=="
|
|
21
|
+
|
|
22
|
+
// Decode Base64 to string
|
|
23
|
+
const decoded = atob("SGVsbG8sIFdvcmxkIQ==");
|
|
24
|
+
console.log(decoded); // "Hello, World!"
|
|
25
|
+
|
|
26
|
+
// Common use case: encoding JSON for transport
|
|
27
|
+
const data = { user: "john", token: "abc123" };
|
|
28
|
+
const base64Data = btoa(JSON.stringify(data));
|
|
29
|
+
|
|
30
|
+
// Decode it back
|
|
31
|
+
const originalData = JSON.parse(atob(base64Data));
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
**Error Handling:**
|
|
35
|
+
|
|
36
|
+
```javascript
|
|
37
|
+
// btoa throws for characters outside Latin1 range (0-255)
|
|
38
|
+
try {
|
|
39
|
+
btoa("Hello 世界"); // Throws DOMException
|
|
40
|
+
} catch (e) {
|
|
41
|
+
console.error("Cannot encode non-Latin1 characters");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// atob throws for invalid Base64
|
|
45
|
+
try {
|
|
46
|
+
atob("not valid base64!!!");
|
|
47
|
+
} catch (e) {
|
|
48
|
+
console.error("Invalid Base64 string");
|
|
49
|
+
}
|
|
50
|
+
```
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// @bun @bun-cjs
|
|
2
|
+
(function(exports, require, module, __filename, __dirname) {var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
7
|
+
var __toCommonJS = (from) => {
|
|
8
|
+
var entry = __moduleCache.get(from), desc;
|
|
9
|
+
if (entry)
|
|
10
|
+
return entry;
|
|
11
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function")
|
|
13
|
+
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
14
|
+
get: () => from[key],
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
}));
|
|
17
|
+
__moduleCache.set(from, entry);
|
|
18
|
+
return entry;
|
|
19
|
+
};
|
|
20
|
+
var __export = (target, all) => {
|
|
21
|
+
for (var name in all)
|
|
22
|
+
__defProp(target, name, {
|
|
23
|
+
get: all[name],
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
set: (newValue) => all[name] = () => newValue
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// packages/encoding/src/base64.ts
|
|
31
|
+
var exports_base64 = {};
|
|
32
|
+
__export(exports_base64, {
|
|
33
|
+
createBtoaFunction: () => createBtoaFunction,
|
|
34
|
+
createAtobFunction: () => createAtobFunction
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(exports_base64);
|
|
37
|
+
var import_quickjs_core = require("@ricsam/quickjs-core");
|
|
38
|
+
function createAtobFunction(context) {
|
|
39
|
+
return import_quickjs_core.defineFunction(context, "atob", (encoded) => {
|
|
40
|
+
const str = String(encoded ?? "");
|
|
41
|
+
try {
|
|
42
|
+
return atob(str);
|
|
43
|
+
} catch {
|
|
44
|
+
throw new DOMException("The string to be decoded is not correctly encoded.", "InvalidCharacterError");
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function createBtoaFunction(context) {
|
|
49
|
+
return import_quickjs_core.defineFunction(context, "btoa", (input) => {
|
|
50
|
+
const str = String(input ?? "");
|
|
51
|
+
for (let i = 0;i < str.length; i++) {
|
|
52
|
+
if (str.charCodeAt(i) > 255) {
|
|
53
|
+
throw new DOMException("The string to be encoded contains characters outside of the Latin1 range.", "InvalidCharacterError");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return btoa(str);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
//# debugId=89685572A9F95C3864756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/base64.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type { QuickJSContext, QuickJSHandle } from \"quickjs-emscripten\";\nimport { defineFunction } from \"@ricsam/quickjs-core\";\n\n/**\n * Create the atob function for QuickJS\n *\n * Decodes a Base64-encoded string.\n *\n * @throws DOMException with \"InvalidCharacterError\" if the input is invalid Base64\n */\nexport function createAtobFunction(context: QuickJSContext): QuickJSHandle {\n return defineFunction(context, \"atob\", (encoded: unknown) => {\n const str = String(encoded ?? \"\");\n\n // Validate and decode using host's atob\n try {\n return atob(str);\n } catch {\n throw new DOMException(\n \"The string to be decoded is not correctly encoded.\",\n \"InvalidCharacterError\"\n );\n }\n });\n}\n\n/**\n * Create the btoa function for QuickJS\n *\n * Encodes a string to Base64.\n *\n * @throws DOMException with \"InvalidCharacterError\" if the string contains\n * characters outside the Latin1 range (0-255)\n */\nexport function createBtoaFunction(context: QuickJSContext): QuickJSHandle {\n return defineFunction(context, \"btoa\", (input: unknown) => {\n const str = String(input ?? \"\");\n\n // Check for characters outside Latin1 range (0-255)\n for (let i = 0; i < str.length; i++) {\n if (str.charCodeAt(i) > 255) {\n throw new DOMException(\n \"The string to be encoded contains characters outside of the Latin1 range.\",\n \"InvalidCharacterError\"\n );\n }\n }\n\n return btoa(str);\n });\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAC+B,IAA/B;AASO,SAAS,kBAAkB,CAAC,SAAwC;AAAA,EACzE,OAAO,mCAAe,SAAS,QAAQ,CAAC,YAAqB;AAAA,IAC3D,MAAM,MAAM,OAAO,WAAW,EAAE;AAAA,IAGhC,IAAI;AAAA,MACF,OAAO,KAAK,GAAG;AAAA,MACf,MAAM;AAAA,MACN,MAAM,IAAI,aACR,sDACA,uBACF;AAAA;AAAA,GAEH;AAAA;AAWI,SAAS,kBAAkB,CAAC,SAAwC;AAAA,EACzE,OAAO,mCAAe,SAAS,QAAQ,CAAC,UAAmB;AAAA,IACzD,MAAM,MAAM,OAAO,SAAS,EAAE;AAAA,IAG9B,SAAS,IAAI,EAAG,IAAI,IAAI,QAAQ,KAAK;AAAA,MACnC,IAAI,IAAI,WAAW,CAAC,IAAI,KAAK;AAAA,QAC3B,MAAM,IAAI,aACR,6EACA,uBACF;AAAA,MACF;AAAA,IACF;AAAA,IAEA,OAAO,KAAK,GAAG;AAAA,GAChB;AAAA;",
|
|
8
|
+
"debugId": "89685572A9F95C3864756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// @bun @bun-cjs
|
|
2
|
+
(function(exports, require, module, __filename, __dirname) {var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __reExport = (target, mod, secondTarget) => {
|
|
7
|
+
for (let key of __getOwnPropNames(mod))
|
|
8
|
+
if (!__hasOwnProp.call(target, key) && key !== "default")
|
|
9
|
+
__defProp(target, key, {
|
|
10
|
+
get: () => mod[key],
|
|
11
|
+
enumerable: true
|
|
12
|
+
});
|
|
13
|
+
if (secondTarget) {
|
|
14
|
+
for (let key of __getOwnPropNames(mod))
|
|
15
|
+
if (!__hasOwnProp.call(secondTarget, key) && key !== "default")
|
|
16
|
+
__defProp(secondTarget, key, {
|
|
17
|
+
get: () => mod[key],
|
|
18
|
+
enumerable: true
|
|
19
|
+
});
|
|
20
|
+
return secondTarget;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
24
|
+
var __toCommonJS = (from) => {
|
|
25
|
+
var entry = __moduleCache.get(from), desc;
|
|
26
|
+
if (entry)
|
|
27
|
+
return entry;
|
|
28
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
29
|
+
if (from && typeof from === "object" || typeof from === "function")
|
|
30
|
+
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
31
|
+
get: () => from[key],
|
|
32
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
33
|
+
}));
|
|
34
|
+
__moduleCache.set(from, entry);
|
|
35
|
+
return entry;
|
|
36
|
+
};
|
|
37
|
+
var __export = (target, all) => {
|
|
38
|
+
for (var name in all)
|
|
39
|
+
__defProp(target, name, {
|
|
40
|
+
get: all[name],
|
|
41
|
+
enumerable: true,
|
|
42
|
+
configurable: true,
|
|
43
|
+
set: (newValue) => all[name] = () => newValue
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// packages/encoding/src/index.ts
|
|
48
|
+
var exports_src = {};
|
|
49
|
+
__export(exports_src, {
|
|
50
|
+
setupEncoding: () => import_setup.setupEncoding
|
|
51
|
+
});
|
|
52
|
+
module.exports = __toCommonJS(exports_src);
|
|
53
|
+
__reExport(exports_src, require("./types.cjs"), module.exports);
|
|
54
|
+
var import_setup = require("./setup.cjs");
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
//# debugId=5F2EF321A5FE813364756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/index.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"// Re-export types\nexport * from \"./types.cjs\";\n\n// Re-export setup function\nexport { setupEncoding } from \"./setup.cjs\";\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA;AAG8B,IAA9B;",
|
|
8
|
+
"debugId": "5F2EF321A5FE813364756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// @bun @bun-cjs
|
|
2
|
+
(function(exports, require, module, __filename, __dirname) {var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
7
|
+
var __toCommonJS = (from) => {
|
|
8
|
+
var entry = __moduleCache.get(from), desc;
|
|
9
|
+
if (entry)
|
|
10
|
+
return entry;
|
|
11
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function")
|
|
13
|
+
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
14
|
+
get: () => from[key],
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
}));
|
|
17
|
+
__moduleCache.set(from, entry);
|
|
18
|
+
return entry;
|
|
19
|
+
};
|
|
20
|
+
var __export = (target, all) => {
|
|
21
|
+
for (var name in all)
|
|
22
|
+
__defProp(target, name, {
|
|
23
|
+
get: all[name],
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
set: (newValue) => all[name] = () => newValue
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// packages/encoding/src/setup.ts
|
|
31
|
+
var exports_setup = {};
|
|
32
|
+
__export(exports_setup, {
|
|
33
|
+
setupEncoding: () => setupEncoding
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(exports_setup);
|
|
36
|
+
var import_quickjs_core = require("@ricsam/quickjs-core");
|
|
37
|
+
var import_base64 = require("./base64.cjs");
|
|
38
|
+
function setupEncoding(context, options = {}) {
|
|
39
|
+
const coreHandle = options.coreHandle ?? import_quickjs_core.setupCore(context, { stateMap: options.stateMap });
|
|
40
|
+
const stateMap = options.stateMap ?? coreHandle.stateMap;
|
|
41
|
+
const atobFn = import_base64.createAtobFunction(context);
|
|
42
|
+
context.setProp(context.global, "atob", atobFn);
|
|
43
|
+
atobFn.dispose();
|
|
44
|
+
const btoaFn = import_base64.createBtoaFunction(context);
|
|
45
|
+
context.setProp(context.global, "btoa", btoaFn);
|
|
46
|
+
btoaFn.dispose();
|
|
47
|
+
return {
|
|
48
|
+
stateMap,
|
|
49
|
+
dispose() {}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
//# debugId=80C831C927BA3F9E64756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/setup.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type { QuickJSContext } from \"quickjs-emscripten\";\nimport { setupCore, createStateMap } from \"@ricsam/quickjs-core\";\nimport type { SetupEncodingOptions, EncodingHandle } from \"./types.cjs\";\nimport { createAtobFunction, createBtoaFunction } from \"./base64.cjs\";\n\n/**\n * Setup encoding globals in a QuickJS context\n *\n * Provides: atob, btoa\n *\n * @example\n * ```typescript\n * const handle = setupEncoding(context);\n *\n * context.evalCode(`\n * const encoded = btoa(\"Hello\");\n * const decoded = atob(encoded);\n * `);\n *\n * handle.dispose();\n * ```\n *\n * @param context - QuickJS context\n * @param options - Setup options\n * @returns EncodingHandle for cleanup\n */\nexport function setupEncoding(\n context: QuickJSContext,\n options: SetupEncodingOptions = {}\n): EncodingHandle {\n // Setup core if not provided (needed for defineFunction infrastructure)\n const coreHandle =\n options.coreHandle ?? setupCore(context, { stateMap: options.stateMap });\n const stateMap = options.stateMap ?? coreHandle.stateMap;\n\n // Create atob function\n const atobFn = createAtobFunction(context);\n context.setProp(context.global, \"atob\", atobFn);\n atobFn.dispose();\n\n // Create btoa function\n const btoaFn = createBtoaFunction(context);\n context.setProp(context.global, \"btoa\", btoaFn);\n btoaFn.dispose();\n\n return {\n stateMap,\n dispose() {\n // Globals are owned by context.global, cleaned up by context.dispose()\n },\n };\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAC0C,IAA1C;AAEuD,IAAvD;AAuBO,SAAS,aAAa,CAC3B,SACA,UAAgC,CAAC,GACjB;AAAA,EAEhB,MAAM,aACJ,QAAQ,cAAc,8BAAU,SAAS,EAAE,UAAU,QAAQ,SAAS,CAAC;AAAA,EACzE,MAAM,WAAW,QAAQ,YAAY,WAAW;AAAA,EAGhD,MAAM,SAAS,iCAAmB,OAAO;AAAA,EACzC,QAAQ,QAAQ,QAAQ,QAAQ,QAAQ,MAAM;AAAA,EAC9C,OAAO,QAAQ;AAAA,EAGf,MAAM,SAAS,iCAAmB,OAAO;AAAA,EACzC,QAAQ,QAAQ,QAAQ,QAAQ,QAAQ,MAAM;AAAA,EAC9C,OAAO,QAAQ;AAAA,EAEf,OAAO;AAAA,IACL;AAAA,IACA,OAAO,GAAG;AAAA,EAGZ;AAAA;",
|
|
8
|
+
"debugId": "80C831C927BA3F9E64756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// @bun @bun-cjs
|
|
2
|
+
(function(exports, require, module, __filename, __dirname) {var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
7
|
+
var __toCommonJS = (from) => {
|
|
8
|
+
var entry = __moduleCache.get(from), desc;
|
|
9
|
+
if (entry)
|
|
10
|
+
return entry;
|
|
11
|
+
entry = __defProp({}, "__esModule", { value: true });
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function")
|
|
13
|
+
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
14
|
+
get: () => from[key],
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
}));
|
|
17
|
+
__moduleCache.set(from, entry);
|
|
18
|
+
return entry;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// packages/encoding/src/types.ts
|
|
22
|
+
var exports_types = {};
|
|
23
|
+
module.exports = __toCommonJS(exports_types);
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
//# debugId=DCCE6E4031E02C5064756E2164756E21
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/encoding/src/base64.ts
|
|
3
|
+
import { defineFunction } from "@ricsam/quickjs-core";
|
|
4
|
+
function createAtobFunction(context) {
|
|
5
|
+
return defineFunction(context, "atob", (encoded) => {
|
|
6
|
+
const str = String(encoded ?? "");
|
|
7
|
+
try {
|
|
8
|
+
return atob(str);
|
|
9
|
+
} catch {
|
|
10
|
+
throw new DOMException("The string to be decoded is not correctly encoded.", "InvalidCharacterError");
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
function createBtoaFunction(context) {
|
|
15
|
+
return defineFunction(context, "btoa", (input) => {
|
|
16
|
+
const str = String(input ?? "");
|
|
17
|
+
for (let i = 0;i < str.length; i++) {
|
|
18
|
+
if (str.charCodeAt(i) > 255) {
|
|
19
|
+
throw new DOMException("The string to be encoded contains characters outside of the Latin1 range.", "InvalidCharacterError");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return btoa(str);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
export {
|
|
26
|
+
createBtoaFunction,
|
|
27
|
+
createAtobFunction
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
//# debugId=F0CAC86FB1CB9DD464756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/base64.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type { QuickJSContext, QuickJSHandle } from \"quickjs-emscripten\";\nimport { defineFunction } from \"@ricsam/quickjs-core\";\n\n/**\n * Create the atob function for QuickJS\n *\n * Decodes a Base64-encoded string.\n *\n * @throws DOMException with \"InvalidCharacterError\" if the input is invalid Base64\n */\nexport function createAtobFunction(context: QuickJSContext): QuickJSHandle {\n return defineFunction(context, \"atob\", (encoded: unknown) => {\n const str = String(encoded ?? \"\");\n\n // Validate and decode using host's atob\n try {\n return atob(str);\n } catch {\n throw new DOMException(\n \"The string to be decoded is not correctly encoded.\",\n \"InvalidCharacterError\"\n );\n }\n });\n}\n\n/**\n * Create the btoa function for QuickJS\n *\n * Encodes a string to Base64.\n *\n * @throws DOMException with \"InvalidCharacterError\" if the string contains\n * characters outside the Latin1 range (0-255)\n */\nexport function createBtoaFunction(context: QuickJSContext): QuickJSHandle {\n return defineFunction(context, \"btoa\", (input: unknown) => {\n const str = String(input ?? \"\");\n\n // Check for characters outside Latin1 range (0-255)\n for (let i = 0; i < str.length; i++) {\n if (str.charCodeAt(i) > 255) {\n throw new DOMException(\n \"The string to be encoded contains characters outside of the Latin1 range.\",\n \"InvalidCharacterError\"\n );\n }\n }\n\n return btoa(str);\n });\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;AACA;AASO,SAAS,kBAAkB,CAAC,SAAwC;AAAA,EACzE,OAAO,eAAe,SAAS,QAAQ,CAAC,YAAqB;AAAA,IAC3D,MAAM,MAAM,OAAO,WAAW,EAAE;AAAA,IAGhC,IAAI;AAAA,MACF,OAAO,KAAK,GAAG;AAAA,MACf,MAAM;AAAA,MACN,MAAM,IAAI,aACR,sDACA,uBACF;AAAA;AAAA,GAEH;AAAA;AAWI,SAAS,kBAAkB,CAAC,SAAwC;AAAA,EACzE,OAAO,eAAe,SAAS,QAAQ,CAAC,UAAmB;AAAA,IACzD,MAAM,MAAM,OAAO,SAAS,EAAE;AAAA,IAG9B,SAAS,IAAI,EAAG,IAAI,IAAI,QAAQ,KAAK;AAAA,MACnC,IAAI,IAAI,WAAW,CAAC,IAAI,KAAK;AAAA,QAC3B,MAAM,IAAI,aACR,6EACA,uBACF;AAAA,MACF;AAAA,IACF;AAAA,IAEA,OAAO,KAAK,GAAG;AAAA,GAChB;AAAA;",
|
|
8
|
+
"debugId": "F0CAC86FB1CB9DD464756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/index.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"// Re-export types\nexport * from \"./types.mjs\";\n\n// Re-export setup function\nexport { setupEncoding } from \"./setup.mjs\";\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;AACA;AAGA;",
|
|
8
|
+
"debugId": "2BCBF4726489F33E64756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// packages/encoding/src/setup.ts
|
|
3
|
+
import { setupCore } from "@ricsam/quickjs-core";
|
|
4
|
+
import { createAtobFunction, createBtoaFunction } from "./base64.mjs";
|
|
5
|
+
function setupEncoding(context, options = {}) {
|
|
6
|
+
const coreHandle = options.coreHandle ?? setupCore(context, { stateMap: options.stateMap });
|
|
7
|
+
const stateMap = options.stateMap ?? coreHandle.stateMap;
|
|
8
|
+
const atobFn = createAtobFunction(context);
|
|
9
|
+
context.setProp(context.global, "atob", atobFn);
|
|
10
|
+
atobFn.dispose();
|
|
11
|
+
const btoaFn = createBtoaFunction(context);
|
|
12
|
+
context.setProp(context.global, "btoa", btoaFn);
|
|
13
|
+
btoaFn.dispose();
|
|
14
|
+
return {
|
|
15
|
+
stateMap,
|
|
16
|
+
dispose() {}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export {
|
|
20
|
+
setupEncoding
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
//# debugId=805B838B0A7777DF64756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/setup.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type { QuickJSContext } from \"quickjs-emscripten\";\nimport { setupCore, createStateMap } from \"@ricsam/quickjs-core\";\nimport type { SetupEncodingOptions, EncodingHandle } from \"./types.mjs\";\nimport { createAtobFunction, createBtoaFunction } from \"./base64.mjs\";\n\n/**\n * Setup encoding globals in a QuickJS context\n *\n * Provides: atob, btoa\n *\n * @example\n * ```typescript\n * const handle = setupEncoding(context);\n *\n * context.evalCode(`\n * const encoded = btoa(\"Hello\");\n * const decoded = atob(encoded);\n * `);\n *\n * handle.dispose();\n * ```\n *\n * @param context - QuickJS context\n * @param options - Setup options\n * @returns EncodingHandle for cleanup\n */\nexport function setupEncoding(\n context: QuickJSContext,\n options: SetupEncodingOptions = {}\n): EncodingHandle {\n // Setup core if not provided (needed for defineFunction infrastructure)\n const coreHandle =\n options.coreHandle ?? setupCore(context, { stateMap: options.stateMap });\n const stateMap = options.stateMap ?? coreHandle.stateMap;\n\n // Create atob function\n const atobFn = createAtobFunction(context);\n context.setProp(context.global, \"atob\", atobFn);\n atobFn.dispose();\n\n // Create btoa function\n const btoaFn = createBtoaFunction(context);\n context.setProp(context.global, \"btoa\", btoaFn);\n btoaFn.dispose();\n\n return {\n stateMap,\n dispose() {\n // Globals are owned by context.global, cleaned up by context.dispose()\n },\n };\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";;AACA;AAEA;AAuBO,SAAS,aAAa,CAC3B,SACA,UAAgC,CAAC,GACjB;AAAA,EAEhB,MAAM,aACJ,QAAQ,cAAc,UAAU,SAAS,EAAE,UAAU,QAAQ,SAAS,CAAC;AAAA,EACzE,MAAM,WAAW,QAAQ,YAAY,WAAW;AAAA,EAGhD,MAAM,SAAS,mBAAmB,OAAO;AAAA,EACzC,QAAQ,QAAQ,QAAQ,QAAQ,QAAQ,MAAM;AAAA,EAC9C,OAAO,QAAQ;AAAA,EAGf,MAAM,SAAS,mBAAmB,OAAO;AAAA,EACzC,QAAQ,QAAQ,QAAQ,QAAQ,QAAQ,MAAM;AAAA,EAC9C,OAAO,QAAQ;AAAA,EAEf,OAAO;AAAA,IACL;AAAA,IACA,OAAO,GAAG;AAAA,EAGZ;AAAA;",
|
|
8
|
+
"debugId": "805B838B0A7777DF64756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { QuickJSContext, QuickJSHandle } from "quickjs-emscripten";
|
|
2
|
+
/**
|
|
3
|
+
* Create the atob function for QuickJS
|
|
4
|
+
*
|
|
5
|
+
* Decodes a Base64-encoded string.
|
|
6
|
+
*
|
|
7
|
+
* @throws DOMException with "InvalidCharacterError" if the input is invalid Base64
|
|
8
|
+
*/
|
|
9
|
+
export declare function createAtobFunction(context: QuickJSContext): QuickJSHandle;
|
|
10
|
+
/**
|
|
11
|
+
* Create the btoa function for QuickJS
|
|
12
|
+
*
|
|
13
|
+
* Encodes a string to Base64.
|
|
14
|
+
*
|
|
15
|
+
* @throws DOMException with "InvalidCharacterError" if the string contains
|
|
16
|
+
* characters outside the Latin1 range (0-255)
|
|
17
|
+
*/
|
|
18
|
+
export declare function createBtoaFunction(context: QuickJSContext): QuickJSHandle;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { QuickJSContext } from "quickjs-emscripten";
|
|
2
|
+
import type { SetupEncodingOptions, EncodingHandle } from "./types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Setup encoding globals in a QuickJS context
|
|
5
|
+
*
|
|
6
|
+
* Provides: atob, btoa
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const handle = setupEncoding(context);
|
|
11
|
+
*
|
|
12
|
+
* context.evalCode(`
|
|
13
|
+
* const encoded = btoa("Hello");
|
|
14
|
+
* const decoded = atob(encoded);
|
|
15
|
+
* `);
|
|
16
|
+
*
|
|
17
|
+
* handle.dispose();
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @param context - QuickJS context
|
|
21
|
+
* @param options - Setup options
|
|
22
|
+
* @returns EncodingHandle for cleanup
|
|
23
|
+
*/
|
|
24
|
+
export declare function setupEncoding(context: QuickJSContext, options?: SetupEncodingOptions): EncodingHandle;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { StateMap, CoreHandle } from "@ricsam/quickjs-core";
|
|
2
|
+
export type { StateMap, CoreHandle };
|
|
3
|
+
/**
|
|
4
|
+
* Options for setupEncoding
|
|
5
|
+
*/
|
|
6
|
+
export interface SetupEncodingOptions {
|
|
7
|
+
/** Shared state map (optional, created if not provided) */
|
|
8
|
+
stateMap?: StateMap;
|
|
9
|
+
/** Existing core handle (optional, setupCore called if not provided) */
|
|
10
|
+
coreHandle?: CoreHandle;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Handle returned from setupEncoding
|
|
14
|
+
*/
|
|
15
|
+
export interface EncodingHandle {
|
|
16
|
+
/** Shared state map */
|
|
17
|
+
readonly stateMap: StateMap;
|
|
18
|
+
/** Dispose all handles and cleanup */
|
|
19
|
+
dispose(): void;
|
|
20
|
+
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ricsam/quickjs-encoding",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
3
|
+
"version": "0.2.6",
|
|
4
|
+
"main": "./dist/cjs/index.cjs",
|
|
5
|
+
"types": "./dist/types/index.d.ts",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/types/index.d.ts",
|
|
9
|
+
"require": "./dist/cjs/index.cjs",
|
|
10
|
+
"import": "./dist/mjs/index.mjs"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "bun build ./src/index.ts --outdir ./dist --target bun",
|
|
15
|
+
"test": "bun test",
|
|
16
|
+
"typecheck": "tsc --noEmit"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@ricsam/quickjs-core": "^0.2.6",
|
|
20
|
+
"quickjs-emscripten": "^0.31.0"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"quickjs-emscripten": "^0.31.0"
|
|
24
|
+
},
|
|
25
|
+
"author": "Richard Samuelsson",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/ricsam/richie-qjs.git"
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/ricsam/richie-qjs/issues"
|
|
33
|
+
},
|
|
34
|
+
"homepage": "https://github.com/ricsam/richie-qjs#readme",
|
|
5
35
|
"keywords": [
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
36
|
+
"quickjs",
|
|
37
|
+
"sandbox",
|
|
38
|
+
"javascript",
|
|
39
|
+
"runtime",
|
|
40
|
+
"fetch",
|
|
41
|
+
"filesystem",
|
|
42
|
+
"streams",
|
|
43
|
+
"wasm",
|
|
44
|
+
"emscripten"
|
|
45
|
+
],
|
|
46
|
+
"description": "Base64 encoding APIs (atob, btoa) for QuickJS runtime",
|
|
47
|
+
"module": "./dist/mjs/index.mjs",
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"files": [
|
|
52
|
+
"dist",
|
|
53
|
+
"README.md"
|
|
9
54
|
]
|
|
10
|
-
}
|
|
55
|
+
}
|