@rebasepro/server-postgresql 0.1.0 → 0.2.1
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/LICENSE +22 -6
- package/dist/common/src/util/entities.d.ts +2 -2
- package/dist/common/src/util/relations.d.ts +1 -1
- package/dist/index.es.js +1250 -1665
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1196 -1611
- package/dist/index.umd.js.map +1 -1
- package/dist/server-postgresql/src/PostgresAdapter.d.ts +6 -0
- package/dist/server-postgresql/src/PostgresBackendDriver.d.ts +2 -1
- package/dist/server-postgresql/src/PostgresBootstrapper.d.ts +0 -5
- package/dist/server-postgresql/src/auth/ensure-tables.d.ts +2 -1
- package/dist/server-postgresql/src/auth/services.d.ts +37 -15
- package/dist/server-postgresql/src/index.d.ts +1 -0
- package/dist/server-postgresql/src/schema/auth-schema.d.ts +43 -856
- package/dist/server-postgresql/src/schema/default-collections.d.ts +2 -0
- package/dist/server-postgresql/src/schema/doctor.d.ts +10 -1
- package/dist/server-postgresql/src/schema/introspect-db-logic.d.ts +1 -0
- package/dist/server-postgresql/src/services/entity-helpers.d.ts +1 -1
- package/dist/server-postgresql/src/services/realtimeService.d.ts +12 -0
- package/dist/server-postgresql/src/websocket.d.ts +2 -1
- package/dist/types/src/controllers/auth.d.ts +9 -8
- package/dist/types/src/controllers/client.d.ts +3 -0
- package/dist/types/src/types/auth_adapter.d.ts +356 -0
- package/dist/types/src/types/collections.d.ts +67 -2
- package/dist/types/src/types/database_adapter.d.ts +94 -0
- package/dist/types/src/types/entity_actions.d.ts +7 -1
- package/dist/types/src/types/entity_callbacks.d.ts +1 -1
- package/dist/types/src/types/entity_views.d.ts +36 -1
- package/dist/types/src/types/index.d.ts +2 -0
- package/dist/types/src/types/plugins.d.ts +1 -1
- package/dist/types/src/types/properties.d.ts +24 -5
- package/dist/types/src/types/property_config.d.ts +6 -2
- package/dist/types/src/types/relations.d.ts +1 -1
- package/dist/types/src/types/translations.d.ts +8 -0
- package/dist/types/src/users/user.d.ts +5 -0
- package/package.json +21 -15
- package/src/PostgresAdapter.ts +59 -0
- package/src/PostgresBackendDriver.ts +57 -8
- package/src/PostgresBootstrapper.ts +35 -15
- package/src/auth/ensure-tables.ts +82 -189
- package/src/auth/services.ts +421 -170
- package/src/cli.ts +44 -13
- package/src/data-transformer.ts +78 -8
- package/src/history/HistoryService.ts +25 -2
- package/src/index.ts +1 -0
- package/src/schema/auth-schema.ts +130 -98
- package/src/schema/default-collections.ts +68 -0
- package/src/schema/doctor-cli.ts +5 -1
- package/src/schema/doctor.ts +85 -8
- package/src/schema/generate-drizzle-schema-logic.ts +74 -27
- package/src/schema/generate-drizzle-schema.ts +13 -3
- package/src/schema/introspect-db-inference.ts +5 -5
- package/src/schema/introspect-db-logic.ts +9 -2
- package/src/schema/introspect-db.ts +14 -3
- package/src/services/EntityFetchService.ts +5 -5
- package/src/services/RelationService.ts +2 -2
- package/src/services/entity-helpers.ts +1 -1
- package/src/services/realtimeService.ts +145 -136
- package/src/utils/drizzle-conditions.ts +16 -2
- package/src/websocket.ts +113 -37
- package/test/auth-services.test.ts +163 -74
- package/test/data-transformer-hardening.test.ts +57 -0
- package/test/data-transformer.test.ts +43 -0
- package/test/generate-drizzle-schema.test.ts +7 -5
- package/test/introspect-db-utils.test.ts +4 -1
- package/test/postgresDataDriver.test.ts +17 -0
- package/test/realtimeService.test.ts +7 -7
- package/test/websocket.test.ts +139 -0
- package/examples/sdk-demo/node_modules/esbuild/LICENSE.md +0 -21
- package/examples/sdk-demo/node_modules/esbuild/README.md +0 -3
- package/examples/sdk-demo/node_modules/esbuild/bin/esbuild +0 -223
- package/examples/sdk-demo/node_modules/esbuild/install.js +0 -289
- package/examples/sdk-demo/node_modules/esbuild/lib/main.d.ts +0 -716
- package/examples/sdk-demo/node_modules/esbuild/lib/main.js +0 -2242
- package/examples/sdk-demo/node_modules/esbuild/package.json +0 -49
|
@@ -172,4 +172,47 @@ path: "authors" }
|
|
|
172
172
|
expect(serializePropertyToServer(false, boolProp)).toBe(false);
|
|
173
173
|
});
|
|
174
174
|
});
|
|
175
|
+
|
|
176
|
+
// ── Vector property ──
|
|
177
|
+
describe("vector property", () => {
|
|
178
|
+
const vectorProp: Property = { type: "vector", dimensions: 3 } as Property;
|
|
179
|
+
|
|
180
|
+
it("should serialize a Vector instance to a flat array", () => {
|
|
181
|
+
const vectorVal = { __type: "Vector", value: [1.5, -2.0, 3.14] };
|
|
182
|
+
expect(serializePropertyToServer(vectorVal, vectorProp)).toEqual([1.5, -2.0, 3.14]);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("should serialize a raw number array to a flat array", () => {
|
|
186
|
+
expect(serializePropertyToServer([0.5, 0.6, 0.7], vectorProp)).toEqual([0.5, 0.6, 0.7]);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("should return null for null values", () => {
|
|
190
|
+
expect(serializePropertyToServer(null, vectorProp)).toBeNull();
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("should return undefined for undefined values", () => {
|
|
194
|
+
expect(serializePropertyToServer(undefined, vectorProp)).toBeUndefined();
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// ── Binary property ──
|
|
199
|
+
describe("binary property", () => {
|
|
200
|
+
const binaryProp: Property = { type: "binary" } as Property;
|
|
201
|
+
|
|
202
|
+
it("should serialize a base64 data URL to a Buffer", () => {
|
|
203
|
+
const base64Data = "data:application/octet-stream;base64,aGVsbG8=";
|
|
204
|
+
const result = serializePropertyToServer(base64Data, binaryProp);
|
|
205
|
+
expect(Buffer.isBuffer(result)).toBe(true);
|
|
206
|
+
expect((result as Buffer).toString("utf8")).toBe("hello");
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("should pass through a Buffer as-is", () => {
|
|
210
|
+
const buffer = Buffer.from("world");
|
|
211
|
+
expect(serializePropertyToServer(buffer, binaryProp)).toBe(buffer);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("should return null for null values", () => {
|
|
215
|
+
expect(serializePropertyToServer(null, binaryProp)).toBeNull();
|
|
216
|
+
});
|
|
217
|
+
});
|
|
175
218
|
});
|
|
@@ -116,6 +116,8 @@ relationName: "tags" }
|
|
|
116
116
|
expect(cleanResult).toContain("tag_id: varchar(\"tag_id\").notNull().references(() => tags.id, { onDelete: \"cascade\" })");
|
|
117
117
|
expect(cleanResult).toContain("(table) => ({ pk: primaryKey({ columns: [table.post_id, table.tag_id] }) })");
|
|
118
118
|
expect(cleanResult).toContain("export const postsRelations = drizzleRelations(posts, ({ one, many }) => ({ \"tags\": many(postsToTags, { relationName: \"tags\" }) }));");
|
|
119
|
+
const expectedJunctionRelations = "export const postsToTagsRelations = drizzleRelations(postsToTags, ({ one, many }) => ({ \"post_id\": one(posts, { fields: [postsToTags.post_id], references: [posts.id], relationName: \"tags\" }), \"tag_id\": one(tags, { fields: [postsToTags.tag_id], references: [tags.id], relationName: \"posts_to_tags_tag_id\" }) }));";
|
|
120
|
+
expect(cleanResult).toContain(cleanSchema(expectedJunctionRelations));
|
|
119
121
|
});
|
|
120
122
|
|
|
121
123
|
describe("generateDrizzleSchema Column Types", () => {
|
|
@@ -391,7 +393,7 @@ ownerField: "user_id" }
|
|
|
391
393
|
expect(result).toContain("pgPolicy");
|
|
392
394
|
expect(result).toContain('as: "permissive"');
|
|
393
395
|
expect(result).toContain('for: "all"');
|
|
394
|
-
expect(result).toContain("
|
|
396
|
+
expect(result).toContain("user_id = auth.uid()");
|
|
395
397
|
// 'all' needs both using and withCheck
|
|
396
398
|
expect(result).toContain("using:");
|
|
397
399
|
expect(result).toContain("withCheck:");
|
|
@@ -565,7 +567,7 @@ using: "{is_locked} = false" }
|
|
|
565
567
|
|
|
566
568
|
const result = await generateSchema(collections);
|
|
567
569
|
expect(result).toContain('as: "restrictive"');
|
|
568
|
-
expect(result).toContain("
|
|
570
|
+
expect(result).toContain("is_locked = false");
|
|
569
571
|
});
|
|
570
572
|
|
|
571
573
|
it("should generate raw SQL using clause with column references", async () => {
|
|
@@ -584,7 +586,7 @@ using: "{published_at} > now() - interval '30 days'" }
|
|
|
584
586
|
}];
|
|
585
587
|
|
|
586
588
|
const result = await generateSchema(collections);
|
|
587
|
-
expect(result).toContain("
|
|
589
|
+
expect(result).toContain("published_at > now() - interval '30 days'");
|
|
588
590
|
});
|
|
589
591
|
|
|
590
592
|
it("should generate raw SQL withCheck clause", async () => {
|
|
@@ -609,8 +611,8 @@ using: "{published_at} > now() - interval '30 days'" }
|
|
|
609
611
|
const result = await generateSchema(collections);
|
|
610
612
|
expect(result).toContain("using:");
|
|
611
613
|
expect(result).toContain("withCheck:");
|
|
612
|
-
expect(result).toContain("
|
|
613
|
-
expect(result).toContain("
|
|
614
|
+
expect(result).toContain("user_id = auth.uid()");
|
|
615
|
+
expect(result).toContain("status != 'archived'");
|
|
614
616
|
});
|
|
615
617
|
|
|
616
618
|
it("should use custom policy names when provided", async () => {
|
|
@@ -200,10 +200,13 @@ describe("mapPgType", () => {
|
|
|
200
200
|
expect(mapPgType("_text")).toBe("array");
|
|
201
201
|
});
|
|
202
202
|
it("maps string-like types to string", () => {
|
|
203
|
-
for (const t of ["text", "varchar", "character varying", "char", "character", "uuid", "
|
|
203
|
+
for (const t of ["text", "varchar", "character varying", "char", "character", "uuid", "inet", "cidr", "macaddr", "macaddr8", "interval"]) {
|
|
204
204
|
expect(mapPgType(t)).toBe("string");
|
|
205
205
|
}
|
|
206
206
|
});
|
|
207
|
+
it("maps bytea to binary", () => {
|
|
208
|
+
expect(mapPgType("bytea")).toBe("binary");
|
|
209
|
+
});
|
|
207
210
|
it("defaults unknown types to string", () => {
|
|
208
211
|
expect(mapPgType("tsvector")).toBe("string");
|
|
209
212
|
expect(mapPgType("xml")).toBe("string");
|
|
@@ -644,5 +644,22 @@ status: "new" });
|
|
|
644
644
|
expect(mockRealtimeService.notifyEntityUpdate).toHaveBeenNthCalledWith(2, "call-2", "2", {}, undefined);
|
|
645
645
|
});
|
|
646
646
|
});
|
|
647
|
+
|
|
648
|
+
describe("PostgresBackendDriver Admin operations", () => {
|
|
649
|
+
it("fetchAvailableRoles should query roles filtered by current user membership", async () => {
|
|
650
|
+
const executeSqlSpy = jest.spyOn(delegate, "executeSql").mockResolvedValueOnce([
|
|
651
|
+
{ rolname: "demo" },
|
|
652
|
+
{ rolname: "cloudsqlsuperuser" }
|
|
653
|
+
]);
|
|
654
|
+
|
|
655
|
+
const result = await delegate.fetchAvailableRoles();
|
|
656
|
+
|
|
657
|
+
expect(executeSqlSpy).toHaveBeenCalledWith(
|
|
658
|
+
"SELECT rolname FROM pg_roles WHERE pg_has_role(current_user, rolname, 'member') ORDER BY rolname;"
|
|
659
|
+
);
|
|
660
|
+
expect(result).toEqual(["demo", "cloudsqlsuperuser"]);
|
|
661
|
+
executeSqlSpy.mockRestore();
|
|
662
|
+
});
|
|
663
|
+
});
|
|
647
664
|
});
|
|
648
665
|
|
|
@@ -3,12 +3,13 @@ import { NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
|
3
3
|
import { PostgresCollectionRegistry } from "../src/collections/PostgresCollectionRegistry";
|
|
4
4
|
import { EntityCollection } from "@rebasepro/types";
|
|
5
5
|
|
|
6
|
+
const mockFetchCollection = jest.fn().mockResolvedValue([{ id: 1, path: "posts", values: { title: "Refetched Title" } }]);
|
|
7
|
+
const mockFetchEntity = jest.fn().mockResolvedValue({ id: 1, path: "posts", values: { title: "Refetched Entity Title" } });
|
|
8
|
+
|
|
6
9
|
jest.mock("../src/services/entityService", () => ({
|
|
7
10
|
EntityService: jest.fn().mockImplementation(() => ({
|
|
8
|
-
fetchCollection:
|
|
9
|
-
|
|
10
|
-
fetchEntity: jest.fn().mockResolvedValue({ id: 1,
|
|
11
|
-
_rebase_invalidated: false }),
|
|
11
|
+
fetchCollection: mockFetchCollection,
|
|
12
|
+
fetchEntity: mockFetchEntity,
|
|
12
13
|
searchEntities: jest.fn().mockResolvedValue([])
|
|
13
14
|
}))
|
|
14
15
|
}));
|
|
@@ -134,7 +135,7 @@ values: { _rebase_invalidated: true } } as any;
|
|
|
134
135
|
await Promise.resolve();
|
|
135
136
|
|
|
136
137
|
// It should fetch the collection with auth
|
|
137
|
-
expect(
|
|
138
|
+
expect(mockFetchCollection).toHaveBeenCalled();
|
|
138
139
|
|
|
139
140
|
// It should send the refetched data
|
|
140
141
|
expect(ws.send).toHaveBeenCalled();
|
|
@@ -207,8 +208,7 @@ values: { _rebase_invalidated: true } } as any;
|
|
|
207
208
|
await Promise.resolve();
|
|
208
209
|
|
|
209
210
|
// It should fetch the single entity
|
|
210
|
-
expect(
|
|
211
|
-
entityId: "1" }));
|
|
211
|
+
expect(mockFetchEntity).toHaveBeenCalledWith("posts", "1", undefined);
|
|
212
212
|
|
|
213
213
|
// It should send entity update
|
|
214
214
|
expect(ws.send).toHaveBeenCalled();
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { describe, it, expect, jest, beforeEach } from "@jest/globals";
|
|
2
|
+
import { WebSocket, WebSocketServer } from "ws";
|
|
3
|
+
import { Server } from "http";
|
|
4
|
+
|
|
5
|
+
let mockWssInstance: any = null;
|
|
6
|
+
|
|
7
|
+
jest.mock("ws", () => {
|
|
8
|
+
return {
|
|
9
|
+
WebSocketServer: jest.fn().mockImplementation(() => {
|
|
10
|
+
const instance = {
|
|
11
|
+
on: jest.fn()
|
|
12
|
+
};
|
|
13
|
+
mockWssInstance = instance;
|
|
14
|
+
return instance;
|
|
15
|
+
}),
|
|
16
|
+
WebSocket: jest.fn()
|
|
17
|
+
};
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
jest.mock("@rebasepro/server-core", () => {
|
|
21
|
+
return {
|
|
22
|
+
extractUserFromToken: jest.fn().mockReturnValue({
|
|
23
|
+
userId: "admin-user",
|
|
24
|
+
roles: ["admin"]
|
|
25
|
+
})
|
|
26
|
+
};
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
import { createPostgresWebSocket } from "../src/websocket";
|
|
30
|
+
import { RealtimeService } from "../src/services/realtimeService";
|
|
31
|
+
import { PostgresBackendDriver } from "../src/PostgresBackendDriver";
|
|
32
|
+
|
|
33
|
+
describe("WebSocket Server SQL error handling", () => {
|
|
34
|
+
let mockServer: Server;
|
|
35
|
+
let mockRealtimeService: RealtimeService;
|
|
36
|
+
let mockDriver: PostgresBackendDriver;
|
|
37
|
+
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
jest.clearAllMocks();
|
|
40
|
+
mockWssInstance = null;
|
|
41
|
+
|
|
42
|
+
mockServer = {} as Server;
|
|
43
|
+
mockRealtimeService = {
|
|
44
|
+
addClient: jest.fn(),
|
|
45
|
+
registerDataDriverSubscription: jest.fn()
|
|
46
|
+
} as unknown as RealtimeService;
|
|
47
|
+
|
|
48
|
+
// Mock PostgresBackendDriver admin capabilities
|
|
49
|
+
mockDriver = {
|
|
50
|
+
key: "postgres",
|
|
51
|
+
initialised: true,
|
|
52
|
+
admin: {
|
|
53
|
+
executeSql: jest.fn()
|
|
54
|
+
}
|
|
55
|
+
} as unknown as PostgresBackendDriver;
|
|
56
|
+
|
|
57
|
+
// Trigger the wss initialization with requireAuth: true
|
|
58
|
+
createPostgresWebSocket(mockServer, mockRealtimeService, mockDriver, { requireAuth: true });
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("should handle EXECUTE_SQL errors cleanly and return ERROR message without throwing", async () => {
|
|
62
|
+
expect(mockWssInstance).toBeDefined();
|
|
63
|
+
expect(mockWssInstance.on).toHaveBeenCalledWith("connection", expect.any(Function));
|
|
64
|
+
|
|
65
|
+
const connectionCallback = mockWssInstance.on.mock.calls.find(
|
|
66
|
+
(call: any[]) => call[0] === "connection"
|
|
67
|
+
)[1];
|
|
68
|
+
|
|
69
|
+
// Simulate client connection
|
|
70
|
+
const mockWs = {
|
|
71
|
+
on: jest.fn(),
|
|
72
|
+
send: jest.fn()
|
|
73
|
+
} as unknown as any;
|
|
74
|
+
|
|
75
|
+
connectionCallback(mockWs);
|
|
76
|
+
|
|
77
|
+
// Retrieve the message callback
|
|
78
|
+
expect(mockWs.on).toHaveBeenCalledWith("message", expect.any(Function));
|
|
79
|
+
const messageCallback = mockWs.on.mock.calls.find(
|
|
80
|
+
(call: any[]) => call[0] === "message"
|
|
81
|
+
)[1];
|
|
82
|
+
|
|
83
|
+
// 1. Authenticate first as an admin
|
|
84
|
+
await messageCallback(
|
|
85
|
+
Buffer.from(
|
|
86
|
+
JSON.stringify({
|
|
87
|
+
type: "AUTHENTICATE",
|
|
88
|
+
requestId: "auth-req",
|
|
89
|
+
payload: {
|
|
90
|
+
token: "mock-admin-token"
|
|
91
|
+
}
|
|
92
|
+
})
|
|
93
|
+
)
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
expect(mockWs.send).toHaveBeenCalled();
|
|
97
|
+
const authResponse = JSON.parse(mockWs.send.mock.calls[0][0]);
|
|
98
|
+
expect(authResponse.type).toBe("AUTH_SUCCESS");
|
|
99
|
+
|
|
100
|
+
// Clear mock send calls before executing SQL
|
|
101
|
+
mockWs.send.mockClear();
|
|
102
|
+
|
|
103
|
+
// Mock executeSql to throw a permission denied error
|
|
104
|
+
(mockDriver.admin.executeSql as jest.Mock).mockRejectedValueOnce(
|
|
105
|
+
new Error("permission denied for table orders")
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
// 2. Simulate receiving EXECUTE_SQL message
|
|
109
|
+
await messageCallback(
|
|
110
|
+
Buffer.from(
|
|
111
|
+
JSON.stringify({
|
|
112
|
+
type: "EXECUTE_SQL",
|
|
113
|
+
requestId: "req-1",
|
|
114
|
+
payload: {
|
|
115
|
+
sql: "SELECT * FROM orders",
|
|
116
|
+
options: { role: "demo" }
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
)
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
// Verify executeSql was called
|
|
123
|
+
expect(mockDriver.admin.executeSql).toHaveBeenCalledWith("SELECT * FROM orders", { role: "demo" });
|
|
124
|
+
|
|
125
|
+
// Verify the client received a clean ERROR payload rather than crashing the socket
|
|
126
|
+
expect(mockWs.send).toHaveBeenCalled();
|
|
127
|
+
const sentMessage = JSON.parse(mockWs.send.mock.calls[0][0]);
|
|
128
|
+
expect(sentMessage).toEqual({
|
|
129
|
+
type: "ERROR",
|
|
130
|
+
requestId: "req-1",
|
|
131
|
+
payload: {
|
|
132
|
+
error: {
|
|
133
|
+
message: "permission denied for table orders",
|
|
134
|
+
code: "SQL_ERROR"
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
});
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020 Evan Wallace
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
@@ -1,223 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
"use strict";
|
|
3
|
-
var __create = Object.create;
|
|
4
|
-
var __defProp = Object.defineProperty;
|
|
5
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
18
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
19
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
20
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
21
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
22
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
23
|
-
mod
|
|
24
|
-
));
|
|
25
|
-
|
|
26
|
-
// lib/npm/node-platform.ts
|
|
27
|
-
var fs = require("fs");
|
|
28
|
-
var os = require("os");
|
|
29
|
-
var path = require("path");
|
|
30
|
-
var ESBUILD_BINARY_PATH = process.env.ESBUILD_BINARY_PATH || ESBUILD_BINARY_PATH;
|
|
31
|
-
var isValidBinaryPath = (x) => !!x && x !== "/usr/bin/esbuild";
|
|
32
|
-
var packageDarwin_arm64 = "@esbuild/darwin-arm64";
|
|
33
|
-
var packageDarwin_x64 = "@esbuild/darwin-x64";
|
|
34
|
-
var knownWindowsPackages = {
|
|
35
|
-
"win32 arm64 LE": "@esbuild/win32-arm64",
|
|
36
|
-
"win32 ia32 LE": "@esbuild/win32-ia32",
|
|
37
|
-
"win32 x64 LE": "@esbuild/win32-x64"
|
|
38
|
-
};
|
|
39
|
-
var knownUnixlikePackages = {
|
|
40
|
-
"aix ppc64 BE": "@esbuild/aix-ppc64",
|
|
41
|
-
"android arm64 LE": "@esbuild/android-arm64",
|
|
42
|
-
"darwin arm64 LE": "@esbuild/darwin-arm64",
|
|
43
|
-
"darwin x64 LE": "@esbuild/darwin-x64",
|
|
44
|
-
"freebsd arm64 LE": "@esbuild/freebsd-arm64",
|
|
45
|
-
"freebsd x64 LE": "@esbuild/freebsd-x64",
|
|
46
|
-
"linux arm LE": "@esbuild/linux-arm",
|
|
47
|
-
"linux arm64 LE": "@esbuild/linux-arm64",
|
|
48
|
-
"linux ia32 LE": "@esbuild/linux-ia32",
|
|
49
|
-
"linux mips64el LE": "@esbuild/linux-mips64el",
|
|
50
|
-
"linux ppc64 LE": "@esbuild/linux-ppc64",
|
|
51
|
-
"linux riscv64 LE": "@esbuild/linux-riscv64",
|
|
52
|
-
"linux s390x BE": "@esbuild/linux-s390x",
|
|
53
|
-
"linux x64 LE": "@esbuild/linux-x64",
|
|
54
|
-
"linux loong64 LE": "@esbuild/linux-loong64",
|
|
55
|
-
"netbsd arm64 LE": "@esbuild/netbsd-arm64",
|
|
56
|
-
"netbsd x64 LE": "@esbuild/netbsd-x64",
|
|
57
|
-
"openbsd arm64 LE": "@esbuild/openbsd-arm64",
|
|
58
|
-
"openbsd x64 LE": "@esbuild/openbsd-x64",
|
|
59
|
-
"sunos x64 LE": "@esbuild/sunos-x64"
|
|
60
|
-
};
|
|
61
|
-
var knownWebAssemblyFallbackPackages = {
|
|
62
|
-
"android arm LE": "@esbuild/android-arm",
|
|
63
|
-
"android x64 LE": "@esbuild/android-x64",
|
|
64
|
-
"openharmony arm64 LE": "@esbuild/openharmony-arm64"
|
|
65
|
-
};
|
|
66
|
-
function pkgAndSubpathForCurrentPlatform() {
|
|
67
|
-
let pkg;
|
|
68
|
-
let subpath;
|
|
69
|
-
let isWASM2 = false;
|
|
70
|
-
let platformKey = `${process.platform} ${os.arch()} ${os.endianness()}`;
|
|
71
|
-
if (platformKey in knownWindowsPackages) {
|
|
72
|
-
pkg = knownWindowsPackages[platformKey];
|
|
73
|
-
subpath = "esbuild.exe";
|
|
74
|
-
} else if (platformKey in knownUnixlikePackages) {
|
|
75
|
-
pkg = knownUnixlikePackages[platformKey];
|
|
76
|
-
subpath = "bin/esbuild";
|
|
77
|
-
} else if (platformKey in knownWebAssemblyFallbackPackages) {
|
|
78
|
-
pkg = knownWebAssemblyFallbackPackages[platformKey];
|
|
79
|
-
subpath = "bin/esbuild";
|
|
80
|
-
isWASM2 = true;
|
|
81
|
-
} else {
|
|
82
|
-
throw new Error(`Unsupported platform: ${platformKey}`);
|
|
83
|
-
}
|
|
84
|
-
return { pkg, subpath, isWASM: isWASM2 };
|
|
85
|
-
}
|
|
86
|
-
function pkgForSomeOtherPlatform() {
|
|
87
|
-
const libMainJS = require.resolve("esbuild");
|
|
88
|
-
const nodeModulesDirectory = path.dirname(path.dirname(path.dirname(libMainJS)));
|
|
89
|
-
if (path.basename(nodeModulesDirectory) === "node_modules") {
|
|
90
|
-
for (const unixKey in knownUnixlikePackages) {
|
|
91
|
-
try {
|
|
92
|
-
const pkg = knownUnixlikePackages[unixKey];
|
|
93
|
-
if (fs.existsSync(path.join(nodeModulesDirectory, pkg))) return pkg;
|
|
94
|
-
} catch {
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
for (const windowsKey in knownWindowsPackages) {
|
|
98
|
-
try {
|
|
99
|
-
const pkg = knownWindowsPackages[windowsKey];
|
|
100
|
-
if (fs.existsSync(path.join(nodeModulesDirectory, pkg))) return pkg;
|
|
101
|
-
} catch {
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return null;
|
|
106
|
-
}
|
|
107
|
-
function downloadedBinPath(pkg, subpath) {
|
|
108
|
-
const esbuildLibDir = path.dirname(require.resolve("esbuild"));
|
|
109
|
-
return path.join(esbuildLibDir, `downloaded-${pkg.replace("/", "-")}-${path.basename(subpath)}`);
|
|
110
|
-
}
|
|
111
|
-
function generateBinPath() {
|
|
112
|
-
if (isValidBinaryPath(ESBUILD_BINARY_PATH)) {
|
|
113
|
-
if (!fs.existsSync(ESBUILD_BINARY_PATH)) {
|
|
114
|
-
console.warn(`[esbuild] Ignoring bad configuration: ESBUILD_BINARY_PATH=${ESBUILD_BINARY_PATH}`);
|
|
115
|
-
} else {
|
|
116
|
-
return { binPath: ESBUILD_BINARY_PATH, isWASM: false };
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
const { pkg, subpath, isWASM: isWASM2 } = pkgAndSubpathForCurrentPlatform();
|
|
120
|
-
let binPath2;
|
|
121
|
-
try {
|
|
122
|
-
binPath2 = require.resolve(`${pkg}/${subpath}`);
|
|
123
|
-
} catch (e) {
|
|
124
|
-
binPath2 = downloadedBinPath(pkg, subpath);
|
|
125
|
-
if (!fs.existsSync(binPath2)) {
|
|
126
|
-
try {
|
|
127
|
-
require.resolve(pkg);
|
|
128
|
-
} catch {
|
|
129
|
-
const otherPkg = pkgForSomeOtherPlatform();
|
|
130
|
-
if (otherPkg) {
|
|
131
|
-
let suggestions = `
|
|
132
|
-
Specifically the "${otherPkg}" package is present but this platform
|
|
133
|
-
needs the "${pkg}" package instead. People often get into this
|
|
134
|
-
situation by installing esbuild on Windows or macOS and copying "node_modules"
|
|
135
|
-
into a Docker image that runs Linux, or by copying "node_modules" between
|
|
136
|
-
Windows and WSL environments.
|
|
137
|
-
|
|
138
|
-
If you are installing with npm, you can try not copying the "node_modules"
|
|
139
|
-
directory when you copy the files over, and running "npm ci" or "npm install"
|
|
140
|
-
on the destination platform after the copy. Or you could consider using yarn
|
|
141
|
-
instead of npm which has built-in support for installing a package on multiple
|
|
142
|
-
platforms simultaneously.
|
|
143
|
-
|
|
144
|
-
If you are installing with yarn, you can try listing both this platform and the
|
|
145
|
-
other platform in your ".yarnrc.yml" file using the "supportedArchitectures"
|
|
146
|
-
feature: https://yarnpkg.com/configuration/yarnrc/#supportedArchitectures
|
|
147
|
-
Keep in mind that this means multiple copies of esbuild will be present.
|
|
148
|
-
`;
|
|
149
|
-
if (pkg === packageDarwin_x64 && otherPkg === packageDarwin_arm64 || pkg === packageDarwin_arm64 && otherPkg === packageDarwin_x64) {
|
|
150
|
-
suggestions = `
|
|
151
|
-
Specifically the "${otherPkg}" package is present but this platform
|
|
152
|
-
needs the "${pkg}" package instead. People often get into this
|
|
153
|
-
situation by installing esbuild with npm running inside of Rosetta 2 and then
|
|
154
|
-
trying to use it with node running outside of Rosetta 2, or vice versa (Rosetta
|
|
155
|
-
2 is Apple's on-the-fly x86_64-to-arm64 translation service).
|
|
156
|
-
|
|
157
|
-
If you are installing with npm, you can try ensuring that both npm and node are
|
|
158
|
-
not running under Rosetta 2 and then reinstalling esbuild. This likely involves
|
|
159
|
-
changing how you installed npm and/or node. For example, installing node with
|
|
160
|
-
the universal installer here should work: https://nodejs.org/en/download/. Or
|
|
161
|
-
you could consider using yarn instead of npm which has built-in support for
|
|
162
|
-
installing a package on multiple platforms simultaneously.
|
|
163
|
-
|
|
164
|
-
If you are installing with yarn, you can try listing both "arm64" and "x64"
|
|
165
|
-
in your ".yarnrc.yml" file using the "supportedArchitectures" feature:
|
|
166
|
-
https://yarnpkg.com/configuration/yarnrc/#supportedArchitectures
|
|
167
|
-
Keep in mind that this means multiple copies of esbuild will be present.
|
|
168
|
-
`;
|
|
169
|
-
}
|
|
170
|
-
throw new Error(`
|
|
171
|
-
You installed esbuild for another platform than the one you're currently using.
|
|
172
|
-
This won't work because esbuild is written with native code and needs to
|
|
173
|
-
install a platform-specific binary executable.
|
|
174
|
-
${suggestions}
|
|
175
|
-
Another alternative is to use the "esbuild-wasm" package instead, which works
|
|
176
|
-
the same way on all platforms. But it comes with a heavy performance cost and
|
|
177
|
-
can sometimes be 10x slower than the "esbuild" package, so you may also not
|
|
178
|
-
want to do that.
|
|
179
|
-
`);
|
|
180
|
-
}
|
|
181
|
-
throw new Error(`The package "${pkg}" could not be found, and is needed by esbuild.
|
|
182
|
-
|
|
183
|
-
If you are installing esbuild with npm, make sure that you don't specify the
|
|
184
|
-
"--no-optional" or "--omit=optional" flags. The "optionalDependencies" feature
|
|
185
|
-
of "package.json" is used by esbuild to install the correct binary executable
|
|
186
|
-
for your current platform.`);
|
|
187
|
-
}
|
|
188
|
-
throw e;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
if (/\.zip\//.test(binPath2)) {
|
|
192
|
-
let pnpapi;
|
|
193
|
-
try {
|
|
194
|
-
pnpapi = require("pnpapi");
|
|
195
|
-
} catch (e) {
|
|
196
|
-
}
|
|
197
|
-
if (pnpapi) {
|
|
198
|
-
const root = pnpapi.getPackageInformation(pnpapi.topLevel).packageLocation;
|
|
199
|
-
const binTargetPath = path.join(
|
|
200
|
-
root,
|
|
201
|
-
"node_modules",
|
|
202
|
-
".cache",
|
|
203
|
-
"esbuild",
|
|
204
|
-
`pnpapi-${pkg.replace("/", "-")}-${"0.27.3"}-${path.basename(subpath)}`
|
|
205
|
-
);
|
|
206
|
-
if (!fs.existsSync(binTargetPath)) {
|
|
207
|
-
fs.mkdirSync(path.dirname(binTargetPath), { recursive: true });
|
|
208
|
-
fs.copyFileSync(binPath2, binTargetPath);
|
|
209
|
-
fs.chmodSync(binTargetPath, 493);
|
|
210
|
-
}
|
|
211
|
-
return { binPath: binTargetPath, isWASM: isWASM2 };
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
return { binPath: binPath2, isWASM: isWASM2 };
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// lib/npm/node-shim.ts
|
|
218
|
-
var { binPath, isWASM } = generateBinPath();
|
|
219
|
-
if (isWASM) {
|
|
220
|
-
require("child_process").execFileSync("node", [binPath].concat(process.argv.slice(2)), { stdio: "inherit" });
|
|
221
|
-
} else {
|
|
222
|
-
require("child_process").execFileSync(binPath, process.argv.slice(2), { stdio: "inherit" });
|
|
223
|
-
}
|