Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "feat(primitives): remove atob/btoa implementation" #790

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/primitives/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
],
"scripts": {
"build": "ts-node scripts/build.ts",
"clean:build": "rm -rf dist abort-controller blob console crypto events fetch streams structured-clone url",
"clean:build": "rm -rf dist abort-controller blob console crypto encoding events fetch streams structured-clone url",
"clean:node": "rm -rf node_modules",
"prebuild": "pnpm run clean:build"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/primitives/src/primitives/encoding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const atob = (enc) => Buffer.from(enc, 'base64').toString('binary')
export const btoa = (str) =>
Buffer.from(String(str), 'binary').toString('base64')
11 changes: 9 additions & 2 deletions packages/primitives/src/primitives/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,20 @@ function requireWithFakeGlobalScope(params) {
export function load(scopedContext = {}) {
/** @type Record<string, any> */
const context = {}
/** @type {import('../../type-definitions/encoding')} */
const encodingImpl = requireWithFakeGlobalScope({
context,
id: 'encoding.js',
sourceCode: injectSourceCode('./encoding.js'),
scopedContext,
})
assign(context, {
TextDecoder,
TextEncoder,
TextEncoderStream,
TextDecoderStream,
atob,
btoa,
atob: encodingImpl.atob,
btoa: encodingImpl.btoa,
performance,
})

Expand Down
7 changes: 2 additions & 5 deletions packages/primitives/type-definitions/encoding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,5 @@ declare const TextDecoderConstructor: typeof TextDecoder
export { TextEncoderConstructor as TextEncoder }
export { TextDecoderConstructor as TextDecoder }

declare const _atob: typeof atob
declare const _btoa: typeof btoa

export { _atob as atob }
export { _btoa as btoa }
export const atob: (encoded: string) => string
export const btoa: (input: any) => string
1 change: 1 addition & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"console/**",
"crypto/**",
"dist/**",
"encoding/**",
"events/**",
"fetch/**",
"streams/**",
Expand Down