Skip to content

Commit

Permalink
Merge branch 'main' of github.com:sass/embedded-host-node into featur…
Browse files Browse the repository at this point in the history
…e.color-4
  • Loading branch information
nex3 committed Sep 11, 2024
2 parents 21c99cb + 71004ad commit 9f10f60
Show file tree
Hide file tree
Showing 33 changed files with 223 additions and 130 deletions.
39 changes: 39 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
## 1.78.0

* The `meta.feature-exists` function is now deprecated. This deprecation is
named `feature-exists`.

* Fix a crash when using `@at-root` without any queries or children in the
indented syntax.

### JS API

* Backport the deprecation options (`fatalDeprecations`, `futureDeprecations`,
and `silenceDeprecations`) to the legacy JS API. The legacy JS API is itself
deprecated, and you should move off of it if possible, but this will allow
users of bundlers and other tools that are still using the legacy API to
still control deprecation warnings.

* Fix a bug where accessing `SourceSpan.url` would crash when a relative URL was
passed to the Sass API.

### Embedded Sass

* Explicitly expose a `sass` executable from the `sass-embedded` npm package.
This was intended to be included in 1.63.0, but due to the way
platform-specific dependency executables are installed it did not work as
intended. Now users can run `npx sass` for local installs or just `sass` when
`sass-embedded` is installed globally.

* Add linux-riscv64, linux-musl-riscv64, and android-riscv64 support for the
`sass-embedded` npm package.

* Fix an edge case where the Dart VM could hang when shutting down when requests
were in flight.

* Fix a race condition where the embedded host could fail to shut down if it was
closed around the same time a new compilation was started.

* Fix a bug where parse-time deprecation warnings could not be controlled by
the deprecation options in some circumstances.

## 1.77.8

* No user-visible changes.
Expand Down
1 change: 1 addition & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const config = {
roots: ['<rootDir>/lib/', '<rootDir>/test/'],
preset: 'ts-jest',
testEnvironment: 'node',
};
Expand Down
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
3 changes: 3 additions & 0 deletions lib/src/legacy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ function convertOptions<sync extends 'sync' | 'async'>(
verbose: options.verbose,
charset: options.charset,
logger: options.logger,
fatalDeprecations: options.fatalDeprecations,
futureDeprecations: options.futureDeprecations,
silenceDeprecations: options.silenceDeprecations,
legacy: true,
};
}
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 9f10f60

Please sign in to comment.