Skip to content

Commit

Permalink
Upgrade protobuf-es to 2.0.0 (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntkme authored Sep 7, 2024
1 parent 9076adb commit 71004ad
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 84 deletions.
15 changes: 10 additions & 5 deletions lib/src/compiler/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import * as p from 'path';
import * as supportsColor from 'supports-color';
import {create} from '@bufbuild/protobuf';

import {Deprecation, deprecations, getDeprecationIds} from '../deprecations';
import {deprotofySourceSpan} from '../deprotofy-span';
import {Dispatcher, DispatcherHandlers} from '../dispatcher';
Expand Down Expand Up @@ -65,7 +67,7 @@ function newCompileRequest(
importers: ImporterRegistry<'sync' | 'async'>,
options?: Options<'sync' | 'async'>
): proto.InboundMessage_CompileRequest {
const request = new proto.InboundMessage_CompileRequest({
const request = create(proto.InboundMessage_CompileRequestSchema, {
importers: importers.importers,
globalFunctions: Object.keys(options?.functions ?? {}),
sourceMap: !!options?.sourceMap,
Expand Down Expand Up @@ -115,7 +117,7 @@ export function newCompileStringRequest(
importers: ImporterRegistry<'sync' | 'async'>,
options?: StringOptions<'sync' | 'async'>
): proto.InboundMessage_CompileRequest {
const input = new proto.InboundMessage_CompileRequest_StringInput({
const input = create(proto.InboundMessage_CompileRequest_StringInputSchema, {
source,
syntax: utils.protofySyntax(options?.syntax ?? 'scss'),
});
Expand All @@ -128,9 +130,12 @@ export function newCompileStringRequest(
if (options && 'importer' in options && options.importer) {
input.importer = importers.register(options.importer);
} else if (url === legacyImporterProtocol) {
input.importer = new proto.InboundMessage_CompileRequest_Importer({
importer: {case: 'path', value: p.resolve('.')},
});
input.importer = create(
proto.InboundMessage_CompileRequest_ImporterSchema,
{
importer: {case: 'path', value: p.resolve('.')},
}
);
} else {
// When importer is not set on the host, the compiler will set a
// FileSystemImporter if `url` is set to a file: url or a NoOpImporter.
Expand Down
5 changes: 3 additions & 2 deletions lib/src/dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import {Observable, Subject} from 'rxjs';
import {filter, map, mergeMap, takeUntil} from 'rxjs/operators';
import {create} from '@bufbuild/protobuf';

import {OutboundResponse} from './messages';
import * as proto from './vendor/embedded_sass_pb';
Expand Down Expand Up @@ -144,7 +145,7 @@ export class Dispatcher<sync extends 'sync' | 'async'> {
try {
this.writeInboundMessage([
this.compilationId,
new proto.InboundMessage({
create(proto.InboundMessageSchema, {
message: {value: request, case: 'compileRequest'},
}),
]);
Expand Down Expand Up @@ -267,7 +268,7 @@ export class Dispatcher<sync extends 'sync' | 'async'> {

this.writeInboundMessage([
this.compilationId,
new proto.InboundMessage({message}),
create(proto.InboundMessageSchema, {message}),
]);
}
}
Expand Down
5 changes: 3 additions & 2 deletions lib/src/function-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// https://opensource.org/licenses/MIT.

import {inspect} from 'util';
import {create} from '@bufbuild/protobuf';

import * as types from './vendor/sass';
import * as utils from './utils';
Expand Down Expand Up @@ -74,15 +75,15 @@ export class FunctionRegistry<sync extends 'sync' | 'async'> {
);
}

return new proto.InboundMessage_FunctionCallResponse({
return create(proto.InboundMessage_FunctionCallResponseSchema, {
result: {case: 'success', value: protofier.protofy(result)},
accessedArgumentLists: protofier.accessedArgumentLists,
});
}
);
},
error =>
new proto.InboundMessage_FunctionCallResponse({
create(proto.InboundMessage_FunctionCallResponseSchema, {
result: {case: 'error', value: `${error}`},
})
);
Expand Down
41 changes: 23 additions & 18 deletions lib/src/importer-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {createRequire} from 'module';
import * as p from 'path';
import {URL} from 'url';
import {inspect} from 'util';
import {create} from '@bufbuild/protobuf';

import {CanonicalizeContext} from './canonicalize-context';
import * as utils from './utils';
Expand Down Expand Up @@ -64,11 +65,10 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
)
)
.concat(
(options?.loadPaths ?? []).map(
path =>
new proto.InboundMessage_CompileRequest_Importer({
importer: {case: 'path', value: p.resolve(path)},
})
(options?.loadPaths ?? []).map(path =>
create(proto.InboundMessage_CompileRequest_ImporterSchema, {
importer: {case: 'path', value: p.resolve(path)},
})
)
);
}
Expand All @@ -77,10 +77,14 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
register(
importer: Importer<sync> | FileImporter<sync> | NodePackageImporter
): proto.InboundMessage_CompileRequest_Importer {
const message = new proto.InboundMessage_CompileRequest_Importer();
const message = create(
proto.InboundMessage_CompileRequest_ImporterSchema,
{}
);
if (importer instanceof NodePackageImporter) {
const importerMessage = new proto.NodePackageImporter();
importerMessage.entryPointDirectory = importer[entryPointDirectoryKey];
const importerMessage = create(proto.NodePackageImporterSchema, {
entryPointDirectory: importer[entryPointDirectoryKey],
});
message.importer = {
case: 'nodePackageImporter',
value: importerMessage,
Expand Down Expand Up @@ -126,7 +130,7 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
return thenOr(
importer.canonicalize(request.url, canonicalizeContext),
url =>
new proto.InboundMessage_CanonicalizeResponse({
create(proto.InboundMessage_CanonicalizeResponseSchema, {
result:
url === null
? {case: undefined}
Expand All @@ -136,7 +140,7 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
);
},
error =>
new proto.InboundMessage_CanonicalizeResponse({
create(proto.InboundMessage_CanonicalizeResponseSchema, {
result: {case: 'error', value: `${error}`},
})
);
Expand All @@ -154,7 +158,8 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
return catchOr(
() => {
return thenOr(importer.load(new URL(request.url)), result => {
if (!result) return new proto.InboundMessage_ImportResponse();
if (!result)
return create(proto.InboundMessage_ImportResponseSchema, {});

if (typeof result.contents !== 'string') {
throw Error(
Expand All @@ -171,20 +176,20 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
);
}

return new proto.InboundMessage_ImportResponse({
return create(proto.InboundMessage_ImportResponseSchema, {
result: {
case: 'success',
value: new proto.InboundMessage_ImportResponse_ImportSuccess({
value: {
contents: result.contents,
syntax: utils.protofySyntax(result.syntax),
sourceMapUrl: result.sourceMapUrl?.toString() ?? '',
}),
},
},
});
});
},
error =>
new proto.InboundMessage_ImportResponse({
create(proto.InboundMessage_ImportResponseSchema, {
result: {case: 'error', value: `${error}`},
})
);
Expand All @@ -210,7 +215,7 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
importer.findFileUrl(request.url, canonicalizeContext),
url => {
if (!url) {
return new proto.InboundMessage_FileImportResponse({
return create(proto.InboundMessage_FileImportResponseSchema, {
containingUrlUnused: !canonicalizeContext.containingUrlAccessed,
});
}
Expand All @@ -220,15 +225,15 @@ export class ImporterRegistry<sync extends 'sync' | 'async'> {
+`"${url}" for URL "${request.url}".`
);
}
return new proto.InboundMessage_FileImportResponse({
return create(proto.InboundMessage_FileImportResponseSchema, {
result: {case: 'fileUrl', value: url.toString()},
containingUrlUnused: !canonicalizeContext.containingUrlAccessed,
});
}
);
},
error =>
new proto.InboundMessage_FileImportResponse({
create(proto.InboundMessage_FileImportResponseSchema, {
result: {case: 'error', value: `${error}`},
})
);
Expand Down
21 changes: 14 additions & 7 deletions lib/src/message-transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import {Observable, Subject} from 'rxjs';
import * as varint from 'varint';
import {create, toBinary} from '@bufbuild/protobuf';

import {expectObservableToError} from '../../test/utils';
import {MessageTransformer} from './message-transformer';
Expand All @@ -13,17 +14,17 @@ describe('message transformer', () => {
let messages: MessageTransformer;

function validInboundMessage(source: string): proto.InboundMessage {
return new proto.InboundMessage({
return create(proto.InboundMessageSchema, {
message: {
case: 'compileRequest',
value: new proto.InboundMessage_CompileRequest({
value: {
input: {
case: 'string',
value: new proto.InboundMessage_CompileRequest_StringInput({
value: {
source,
}),
},
},
}),
},
},
});
}
Expand All @@ -42,7 +43,10 @@ describe('message transformer', () => {
const message = validInboundMessage('a {b: c}');
messages.writeInboundMessage([1234, message]);
expect(encodedProtobufs).toEqual([
Uint8Array.from([...varint.encode(1234), ...message.toBinary()]),
Uint8Array.from([
...varint.encode(1234),
...toBinary(proto.InboundMessageSchema, message),
]),
]);
});
});
Expand Down Expand Up @@ -81,7 +85,10 @@ describe('message transformer', () => {
protobufs$.next(
Uint8Array.from([
...varint.encode(1234),
...validInboundMessage('a {b: c}').toBinary(),
...toBinary(
proto.InboundMessageSchema,
validInboundMessage('a {b: c}')
),
])
);
protobufs$.complete();
Expand Down
13 changes: 10 additions & 3 deletions lib/src/message-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@

import {Observable, Subject} from 'rxjs';
import {map} from 'rxjs/operators';
import {fromBinary, toBinary} from '@bufbuild/protobuf';
import * as varint from 'varint';

import {compilerError} from './utils';
import {InboundMessage, OutboundMessage} from './vendor/embedded_sass_pb';
import {
InboundMessage,
InboundMessageSchema,
OutboundMessage,
OutboundMessageSchema,
} from './vendor/embedded_sass_pb';

/**
* Encodes InboundMessages into protocol buffers and decodes protocol buffers
Expand Down Expand Up @@ -43,7 +49,7 @@ export class MessageTransformer {
InboundMessage,
]): void {
const compilationIdLength = varint.encodingLength(compilationId);
const encodedMessage = message.toBinary();
const encodedMessage = toBinary(InboundMessageSchema, message);
const buffer = new Uint8Array(compilationIdLength + encodedMessage.length);
varint.encode(compilationId, buffer);
buffer.set(encodedMessage, compilationIdLength);
Expand Down Expand Up @@ -71,7 +77,8 @@ function decode(buffer: Uint8Array): [number, OutboundMessage] {
try {
return [
compilationId,
OutboundMessage.fromBinary(
fromBinary(
OutboundMessageSchema,
new Uint8Array(buffer.buffer, varint.decode.bytes)
),
];
Expand Down
Loading

0 comments on commit 71004ad

Please sign in to comment.