-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
The generated bytesFromBase64 and base64FromBytes functions now only include code that's required for the specified env: For env=node, the functions now exclusively use globalThis.Buffer to de-/encode to and from JSON For env=browser, globalThis.btoa/atob is used For env=both, the two functions use either the node or browser implementations depending on whether globalThis.Buffer exists
- Loading branch information
1 parent
061eaf8
commit 0d34612
Showing
13 changed files
with
271 additions
and
57 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
integration/bytes-as-base64-browser/bytes-as-base64-browser-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Message } from './message'; | ||
|
||
describe('bytes-as-base64', () => { | ||
type TestData = [string, Uint8Array]; | ||
const testData: TestData[] = [ | ||
['3q2+7w==', Uint8Array.from([0xDE, 0xAD, 0xBE, 0xEF])], | ||
['AAAAAAAAAAAAAAAAAAAAAA==', new Uint8Array(16).fill(0x00)], | ||
['/////////////////////w==', new Uint8Array(16).fill(0xFF)], | ||
['AAECAwQFBgcICQoLDA0ODw==', new Uint8Array(16).map((_, i) => i)], | ||
]; | ||
|
||
it('fromJSON can decode bytes from base64', () => { | ||
for (const entry of testData) { | ||
const message = Message.fromJSON({ data: entry[0] }); | ||
expect(message).toEqual({ data: entry[1] }); | ||
} | ||
}); | ||
|
||
it('toJSON can encode bytes as base64', () => { | ||
for (const entry of testData) { | ||
const message = Message.toJSON({ data: entry[1] }); | ||
expect(message).toEqual({ data: entry[0] }); | ||
} | ||
}); | ||
|
||
it('fromJSON and toJSON can handle "large" bytes fields', () => { | ||
const LENGTH = 1000000; // 1 MB | ||
const messageA = { data: new Uint8Array(LENGTH).fill(0xFF) }; | ||
const json = Message.toJSON(messageA); | ||
expect(json).toHaveProperty('data'); | ||
const messageB = Message.fromJSON(json); | ||
expect(messageA).toEqual(messageB); | ||
}); | ||
}); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
syntax = "proto3"; | ||
|
||
message Message { | ||
bytes data = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* eslint-disable */ | ||
|
||
export const protobufPackage = ""; | ||
|
||
export interface Message { | ||
data: Uint8Array; | ||
} | ||
|
||
function createBaseMessage(): Message { | ||
return { data: new Uint8Array(0) }; | ||
} | ||
|
||
export const Message = { | ||
fromJSON(object: any): Message { | ||
return { data: isSet(object.data) ? bytesFromBase64(object.data) : new Uint8Array(0) }; | ||
}, | ||
|
||
toJSON(message: Message): unknown { | ||
const obj: any = {}; | ||
if (message.data.length !== 0) { | ||
obj.data = base64FromBytes(message.data); | ||
} | ||
return obj; | ||
}, | ||
|
||
create<I extends Exact<DeepPartial<Message>, I>>(base?: I): Message { | ||
return Message.fromPartial(base ?? ({} as any)); | ||
}, | ||
fromPartial<I extends Exact<DeepPartial<Message>, I>>(object: I): Message { | ||
const message = createBaseMessage(); | ||
message.data = object.data ?? new Uint8Array(0); | ||
return message; | ||
}, | ||
}; | ||
|
||
function bytesFromBase64(b64: string): Uint8Array { | ||
const bin = globalThis.atob(b64); | ||
const arr = new Uint8Array(bin.length); | ||
for (let i = 0; i < bin.length; ++i) { | ||
arr[i] = bin.charCodeAt(i); | ||
} | ||
return arr; | ||
} | ||
|
||
function base64FromBytes(arr: Uint8Array): string { | ||
const bin: string[] = []; | ||
arr.forEach((byte) => { | ||
bin.push(globalThis.String.fromCharCode(byte)); | ||
}); | ||
return globalThis.btoa(bin.join("")); | ||
} | ||
|
||
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; | ||
|
||
export type DeepPartial<T> = T extends Builtin ? T | ||
: T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>> | ||
: T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> | ||
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> } | ||
: Partial<T>; | ||
|
||
type KeysOfUnion<T> = T extends T ? keyof T : never; | ||
export type Exact<P, I extends P> = P extends Builtin ? P | ||
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never }; | ||
|
||
function isSet(value: any): boolean { | ||
return value !== null && value !== undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
outputEncodeMethods=false,outputJsonMethods=true,env=browser |
34 changes: 34 additions & 0 deletions
34
integration/bytes-as-base64-node/bytes-as-base64-node-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Message } from './message'; | ||
|
||
describe('bytes-as-base64', () => { | ||
type TestData = [string, Buffer]; | ||
const testData: TestData[] = [ | ||
['3q2+7w==', Buffer.from([0xDE, 0xAD, 0xBE, 0xEF])], | ||
['AAAAAAAAAAAAAAAAAAAAAA==', Buffer.alloc(16).fill(0x00)], | ||
['/////////////////////w==', Buffer.alloc(16).fill(0xFF)], | ||
['AAECAwQFBgcICQoLDA0ODw==', Buffer.from(Array.from({length: 16}).map((_, i) => i))], | ||
]; | ||
|
||
it('fromJSON can decode bytes from base64', () => { | ||
for (const entry of testData) { | ||
const message = Message.fromJSON({ data: entry[0] }); | ||
expect(message).toEqual({ data: entry[1] }); | ||
} | ||
}); | ||
|
||
it('toJSON can encode bytes as base64', () => { | ||
for (const entry of testData) { | ||
const message = Message.toJSON({ data: entry[1] }); | ||
expect(message).toEqual({ data: entry[0] }); | ||
} | ||
}); | ||
|
||
it('fromJSON and toJSON can handle "large" bytes fields', () => { | ||
const LENGTH = 1000000; // 1 MB | ||
const messageA = { data: Buffer.alloc(LENGTH).fill(0xFF) }; | ||
const json = Message.toJSON(messageA); | ||
expect(json).toHaveProperty('data'); | ||
const messageB = Message.fromJSON(json); | ||
expect(messageA).toEqual(messageB); | ||
}); | ||
}); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
syntax = "proto3"; | ||
|
||
message Message { | ||
bytes data = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* eslint-disable */ | ||
|
||
export const protobufPackage = ""; | ||
|
||
export interface Message { | ||
data: Buffer; | ||
} | ||
|
||
function createBaseMessage(): Message { | ||
return { data: Buffer.alloc(0) }; | ||
} | ||
|
||
export const Message = { | ||
fromJSON(object: any): Message { | ||
return { data: isSet(object.data) ? Buffer.from(bytesFromBase64(object.data)) : Buffer.alloc(0) }; | ||
}, | ||
|
||
toJSON(message: Message): unknown { | ||
const obj: any = {}; | ||
if (message.data.length !== 0) { | ||
obj.data = base64FromBytes(message.data); | ||
} | ||
return obj; | ||
}, | ||
|
||
create<I extends Exact<DeepPartial<Message>, I>>(base?: I): Message { | ||
return Message.fromPartial(base ?? ({} as any)); | ||
}, | ||
fromPartial<I extends Exact<DeepPartial<Message>, I>>(object: I): Message { | ||
const message = createBaseMessage(); | ||
message.data = object.data ?? Buffer.alloc(0); | ||
return message; | ||
}, | ||
}; | ||
|
||
function bytesFromBase64(b64: string): Uint8Array { | ||
return Uint8Array.from(globalThis.Buffer.from(b64, "base64")); | ||
} | ||
|
||
function base64FromBytes(arr: Uint8Array): string { | ||
return globalThis.Buffer.from(arr).toString("base64"); | ||
} | ||
|
||
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; | ||
|
||
export type DeepPartial<T> = T extends Builtin ? T | ||
: T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>> | ||
: T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> | ||
: T extends {} ? { [K in keyof T]?: DeepPartial<T[K]> } | ||
: Partial<T>; | ||
|
||
type KeysOfUnion<T> = T extends T ? keyof T : never; | ||
export type Exact<P, I extends P> = P extends Builtin ? P | ||
: P & { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P>>]: never }; | ||
|
||
function isSet(value: any): boolean { | ||
return value !== null && value !== undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
outputEncodeMethods=false,outputJsonMethods=true,env=node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters