|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { initialize, load, resolve } from './loader-hooks'; |
| 10 | +import { createSharedServerFiles } from './utils'; |
| 11 | + |
| 12 | +describe('esm-in-memory-loader loader-hooks', () => { |
| 13 | + const workspaceRoot = '/mock/workspace/root'; |
| 14 | + const sharedFiles = createSharedServerFiles({ |
| 15 | + 'main.server.mjs': 'export const main = true;', |
| 16 | + 'chunk-abc.mjs': 'export const chunk = "abc";', |
| 17 | + 'nested/chunk-sub.mjs': 'export const sub = "sub";', |
| 18 | + 'utf8.mjs': 'export const text = "🔥 UTF-8 🚀";', |
| 19 | + 'empty.mjs': '', |
| 20 | + }); |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + initialize({ |
| 24 | + workspaceRoot, |
| 25 | + outputFiles: sharedFiles, |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('resolve', () => { |
| 30 | + it('should resolve memory:// URLs into virtual filesystem URLs', () => { |
| 31 | + const nextResolve = jasmine.createSpy('nextResolve'); |
| 32 | + const memoryUrl = new URL('./main.server.mjs', 'memory://').href; |
| 33 | + const result = resolve(memoryUrl, { parentURL: undefined }, nextResolve); |
| 34 | + |
| 35 | + expect(nextResolve).not.toHaveBeenCalled(); |
| 36 | + expect(result.format).toBe('module'); |
| 37 | + expect(result.shortCircuit).toBeTrue(); |
| 38 | + expect(result.url).toContain('/.angular/prerender-root/'); |
| 39 | + expect(result.url).toContain('/main.server.mjs'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should fail when memory:// URL is malformed', () => { |
| 43 | + const nextResolve = jasmine.createSpy('nextResolve'); |
| 44 | + expect(() => { |
| 45 | + resolve('memory://::invalid', { parentURL: undefined }, nextResolve); |
| 46 | + }).toThrowMatching((err: Error) => |
| 47 | + err.message.includes('External code attempted to use malformed memory scheme'), |
| 48 | + ); |
| 49 | + expect(nextResolve).not.toHaveBeenCalled(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should resolve relative specifiers within in-memory files', () => { |
| 53 | + const memoryUrl = new URL('./main.server.mjs', 'memory://').href; |
| 54 | + const rootResolve = resolve(memoryUrl, { parentURL: undefined }, () => {}); |
| 55 | + const parentURL = rootResolve.url; |
| 56 | + |
| 57 | + const nextResolve = jasmine.createSpy('nextResolve'); |
| 58 | + const result = resolve('./chunk-abc.mjs', { parentURL }, nextResolve); |
| 59 | + |
| 60 | + expect(nextResolve).not.toHaveBeenCalled(); |
| 61 | + expect(result.format).toBe('module'); |
| 62 | + expect(result.shortCircuit).toBeTrue(); |
| 63 | + expect(result.url).toContain('/chunk-abc.mjs'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should resolve relative specifiers navigating parent directories within in-memory files', () => { |
| 67 | + const memoryUrl = new URL('./nested/chunk-sub.mjs', 'memory://').href; |
| 68 | + const rootResolve = resolve(memoryUrl, { parentURL: undefined }, () => {}); |
| 69 | + const parentURL = rootResolve.url; |
| 70 | + |
| 71 | + const nextResolve = jasmine.createSpy('nextResolve'); |
| 72 | + const result = resolve('../chunk-abc.mjs', { parentURL }, nextResolve); |
| 73 | + |
| 74 | + expect(nextResolve).not.toHaveBeenCalled(); |
| 75 | + expect(result.format).toBe('module'); |
| 76 | + expect(result.shortCircuit).toBeTrue(); |
| 77 | + expect(result.url).toContain('/chunk-abc.mjs'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should fail when relative specifier from in-memory file does not exist', () => { |
| 81 | + const memoryUrl = new URL('./main.server.mjs', 'memory://').href; |
| 82 | + const rootResolve = resolve(memoryUrl, { parentURL: undefined }, () => {}); |
| 83 | + const parentURL = rootResolve.url; |
| 84 | + |
| 85 | + const nextResolve = jasmine.createSpy('nextResolve'); |
| 86 | + expect(() => { |
| 87 | + resolve('./non-existent.mjs', { parentURL }, nextResolve); |
| 88 | + }).toThrowMatching((err: Error) => |
| 89 | + err.message.includes('In-memory ESM relative file should always exist'), |
| 90 | + ); |
| 91 | + expect(nextResolve).not.toHaveBeenCalled(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should rewrite parentURL to index.js in virtual root for bare package specifiers', () => { |
| 95 | + const memoryUrl = new URL('./main.server.mjs', 'memory://').href; |
| 96 | + const rootResolve = resolve(memoryUrl, { parentURL: undefined }, () => {}); |
| 97 | + const parentURL = rootResolve.url; |
| 98 | + |
| 99 | + const nextResolve = jasmine |
| 100 | + .createSpy('nextResolve') |
| 101 | + .and.returnValue({ url: 'file:///some/node_modules/@angular/core/index.js' }); |
| 102 | + const result = resolve('@angular/core', { parentURL }, nextResolve); |
| 103 | + |
| 104 | + expect(nextResolve).toHaveBeenCalledWith( |
| 105 | + '@angular/core', |
| 106 | + jasmine.objectContaining({ |
| 107 | + parentURL: jasmine.stringMatching(/\/\.angular\/prerender-root\/[^/]+\/index\.js$/), |
| 108 | + }), |
| 109 | + ); |
| 110 | + expect(result.url).toBe('file:///some/node_modules/@angular/core/index.js'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should delegate to nextResolve for external non-memory URLs', () => { |
| 114 | + const nextResolve = jasmine |
| 115 | + .createSpy('nextResolve') |
| 116 | + .and.returnValue({ url: 'file:///some/ext/pkg' }); |
| 117 | + const result = resolve('some-pkg', { parentURL: 'file:///some/ext/file.js' }, nextResolve); |
| 118 | + |
| 119 | + expect(nextResolve).toHaveBeenCalledWith('some-pkg', { |
| 120 | + parentURL: 'file:///some/ext/file.js', |
| 121 | + }); |
| 122 | + expect(result.url).toBe('file:///some/ext/pkg'); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + describe('load', () => { |
| 127 | + it('should load in-memory file source from SharedArrayBuffer backed Uint8Array', async () => { |
| 128 | + const memoryUrl = new URL('./main.server.mjs', 'memory://').href; |
| 129 | + const rootResolve = resolve(memoryUrl, { parentURL: undefined }, () => {}); |
| 130 | + const nextLoad = jasmine.createSpy('nextLoad'); |
| 131 | + |
| 132 | + const result = await load(rootResolve.url, { format: 'module' }, nextLoad); |
| 133 | + |
| 134 | + expect(nextLoad).not.toHaveBeenCalled(); |
| 135 | + expect(result.format).toBe('module'); |
| 136 | + expect(result.shortCircuit).toBeTrue(); |
| 137 | + expect(result.source).toBe('export const main = true;'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should load in-memory file with multi-byte UTF-8 characters', async () => { |
| 141 | + const memoryUrl = new URL('./utf8.mjs', 'memory://').href; |
| 142 | + const rootResolve = resolve(memoryUrl, { parentURL: undefined }, () => {}); |
| 143 | + const nextLoad = jasmine.createSpy('nextLoad'); |
| 144 | + |
| 145 | + const result = await load(rootResolve.url, { format: 'module' }, nextLoad); |
| 146 | + |
| 147 | + expect(nextLoad).not.toHaveBeenCalled(); |
| 148 | + expect(result.format).toBe('module'); |
| 149 | + expect(result.shortCircuit).toBeTrue(); |
| 150 | + expect(result.source).toBe('export const text = "🔥 UTF-8 🚀";'); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should load in-memory file with empty content', async () => { |
| 154 | + const memoryUrl = new URL('./empty.mjs', 'memory://').href; |
| 155 | + const rootResolve = resolve(memoryUrl, { parentURL: undefined }, () => {}); |
| 156 | + const nextLoad = jasmine.createSpy('nextLoad'); |
| 157 | + |
| 158 | + const result = await load(rootResolve.url, { format: 'module' }, nextLoad); |
| 159 | + |
| 160 | + expect(nextLoad).not.toHaveBeenCalled(); |
| 161 | + expect(result.format).toBe('module'); |
| 162 | + expect(result.shortCircuit).toBeTrue(); |
| 163 | + expect(result.source).toBe(''); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should load in-memory file source with non-zero byteOffset in Uint8Array', async () => { |
| 167 | + const target = 'export const sliced = 42;'; |
| 168 | + const fullBuffer = Buffer.from(`__PADDING__${target}__MORE__`); |
| 169 | + const offset = Buffer.byteLength('__PADDING__', 'utf-8'); |
| 170 | + const length = Buffer.byteLength(target, 'utf-8'); |
| 171 | + const subView = new Uint8Array(fullBuffer.buffer, fullBuffer.byteOffset + offset, length); |
| 172 | + |
| 173 | + initialize({ |
| 174 | + workspaceRoot, |
| 175 | + outputFiles: { |
| 176 | + 'sliced.mjs': subView, |
| 177 | + }, |
| 178 | + }); |
| 179 | + |
| 180 | + const memoryUrl = new URL('./sliced.mjs', 'memory://').href; |
| 181 | + const rootResolve = resolve(memoryUrl, { parentURL: undefined }, () => {}); |
| 182 | + const nextLoad = jasmine.createSpy('nextLoad'); |
| 183 | + |
| 184 | + const result = await load(rootResolve.url, { format: 'module' }, nextLoad); |
| 185 | + |
| 186 | + expect(nextLoad).not.toHaveBeenCalled(); |
| 187 | + expect(result.format).toBe('module'); |
| 188 | + expect(result.shortCircuit).toBeTrue(); |
| 189 | + expect(result.source).toBe(target); |
| 190 | + }); |
| 191 | + |
| 192 | + it('should load in-memory file source when outputFiles contain string values', async () => { |
| 193 | + initialize({ |
| 194 | + workspaceRoot, |
| 195 | + outputFiles: { |
| 196 | + 'string-file.mjs': 'export const fromString = 1;', |
| 197 | + }, |
| 198 | + }); |
| 199 | + |
| 200 | + const memoryUrl = new URL('./string-file.mjs', 'memory://').href; |
| 201 | + const rootResolve = resolve(memoryUrl, { parentURL: undefined }, () => {}); |
| 202 | + const nextLoad = jasmine.createSpy('nextLoad'); |
| 203 | + |
| 204 | + const result = await load(rootResolve.url, { format: 'module' }, nextLoad); |
| 205 | + |
| 206 | + expect(nextLoad).not.toHaveBeenCalled(); |
| 207 | + expect(result.format).toBe('module'); |
| 208 | + expect(result.shortCircuit).toBeTrue(); |
| 209 | + expect(result.source).toBe('export const fromString = 1;'); |
| 210 | + }); |
| 211 | + |
| 212 | + it('should reject when in-memory file does not exist in outputFiles', async () => { |
| 213 | + const memoryUrl = new URL('./main.server.mjs', 'memory://').href; |
| 214 | + const rootResolve = resolve(memoryUrl, { parentURL: undefined }, () => {}); |
| 215 | + const nonExistentVirtualUrl = rootResolve.url.replace('main.server.mjs', 'missing.mjs'); |
| 216 | + const nextLoad = jasmine.createSpy('nextLoad'); |
| 217 | + |
| 218 | + await expectAsync( |
| 219 | + load(nonExistentVirtualUrl, { format: 'module' }, nextLoad), |
| 220 | + ).toBeRejectedWithError(/Resolved in-memory ESM file should always exist/); |
| 221 | + expect(nextLoad).not.toHaveBeenCalled(); |
| 222 | + }); |
| 223 | + |
| 224 | + it('should delegate to nextLoad for non-angular file URLs', async () => { |
| 225 | + const nextLoad = jasmine.createSpy('nextLoad').and.resolveTo({ format: 'module' }); |
| 226 | + const result = await load( |
| 227 | + 'file:///workspace/node_modules/rxjs/index.js', |
| 228 | + { format: 'module' }, |
| 229 | + nextLoad, |
| 230 | + ); |
| 231 | + |
| 232 | + expect(nextLoad).toHaveBeenCalledWith('file:///workspace/node_modules/rxjs/index.js', { |
| 233 | + format: 'module', |
| 234 | + }); |
| 235 | + expect(result.format).toBe('module'); |
| 236 | + }); |
| 237 | + |
| 238 | + it('should delegate to nextLoad for non-memory non-file URLs', async () => { |
| 239 | + const nextLoad = jasmine.createSpy('nextLoad').and.resolveTo({ format: 'builtin' }); |
| 240 | + const result = await load('node:fs', { format: 'builtin' }, nextLoad); |
| 241 | + |
| 242 | + expect(nextLoad).toHaveBeenCalledWith('node:fs'); |
| 243 | + expect(result.format).toBe('builtin'); |
| 244 | + }); |
| 245 | + }); |
| 246 | +}); |
0 commit comments